Can more than one id be placed in a single input type text?


my question is if you can place more than one id in an input because I have a function in javacript that I send it to call in the id of an input type text but in that input I also have to have the id with the variable in my database. where the data will be inserted.

this is the input code:

  <h5>Avance 1:</h5>
  <input type="text" name="CONTRATO" id="total" size="5" value="<%= rs.getString(5) %>" disabled="0">

the name of my Javascript function is total and the variable in my database is called contrato

How could I do it in that case?

asked by Manny Brenes 22.02.2017 в 20:56

6 answers


I leave an example with a div that has 2 id, the first call with the first id works, but when the second div is called there is an error !!! }

conclusion: there can not be 2 id in an element.

var texto=document.getElementById("uno").innerHTML;

var texto=document.getElementById("primero").innerHTML;
<div id="uno" id="primero"> 1</div>
answered by 22.02.2017 / 21:09

By answering the question, NO you can place more than one id on an element and this id must be unique throughout the document.

One solution to assign more data in the element is to use the data attribute. For example you can create inputs like this:

<input id="input_1" data-type="total" data-name="contract" type="text" size="5" value="1" disabled="0">

And with Javascript you would get the values as follows:

var myInput = document.getElementById('input_1');

myInput.dataset.type // "total" // "contract"
answered by 22.02.2017 в 21:35

By definition a function, object, class, etc. must have a unique name which serves as an identifier for future references and calls that you wish to make as the case.

What can be done (but I do not recommend it) is to use a numerator for the identifier, for example:

string variable1;
string variable2;

What is done here is to have two variables with the same id only add a numerator, in practice this is not recommended because it can cause misunderstandings later and possible bugs.

I hope the clarification will be useful, greetings.

P.D.You can read this article about Identifiers if you're interested.

answered by 22.02.2017 в 21:10

As you already commented definitely you can not have the same ID for different elements is a unique identifier and unrepeatable, what you could do is put another input and hide it and then send the ID you need and ready.

answered by 22.02.2017 в 21:45

I see that you were all told that you can NOT put two ids that is correct but you can create a new attribute objectid for example and play with that

<input type="text" name="CONTRATO" id="total" objectid="fb13a2ac-bd10-db11-b07d-000fea72363c" size="5" value="<%= rs.getString(5) %>" disabled="0">
answered by 24.02.2017 в 12:40

What you can use is the name property.

<input id="usandoId" name="usandoName" type="text" />
answered by 22.02.2017 в 21:33