Take the ID value and play in a variable

Asked

Viewed 74 times

1

Well my problem is that I am working with table Father x Son and when I create a new line, all inputs receive different Ids and needed to pass what is inside the ID to a variable

Example:

<input type="text" id="Total___1" name="Total_Soma">
var idtostring = "Total___1"

With this I already solve my problem, but I think I see no possibility for this

NOTE: I do not create the ID, it fits the line, so it is no use to say that it is just do the functions for this id, because they change and are N possibilities.

  • Matheus even edited the answer right now, just to put the ID in a variable before making the console.log direct with the ID, I think it’s closer than you need it there.

1 answer

0


Makes a forEach catching the ids. Ressalve is to leave the script after the script that does the loop of the elements receiving the ID, or always run this script along with the function that does the loop and creates the ID, only this script has to come at the end of this function.

NOTE: I put in the inputs inside the table the attribute [data-input] just to facilitate to pick up only these particular inputs.

let meuID = document.querySelectorAll('[data-input]');

meuID.forEach( (e) => {
    let qualID = e.id;
    console.log(qualID)
})
<table>
    <tr>
        <td>
            <input data-input type="text" id="n1" name="Total_Soma">
        </td>
        <td>
            <input data-input type="text" id="n2" name="Total_Soma">
        </td>
    </tr>
    <tr>
        <td>
            <input data-input type="text" id="n3" name="Total_Soma">
        </td>
        <td>
            <input data-input type="text" id="n4" name="Total_Soma">
        </td>
    </tr>
</table>

  • 1

    Thank you very much!! It will help me a lot!!

  • @Matheusatar without problems my dear I’m glad to have helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.