Can I use the same Id I set for Name in HTML?

Asked

Viewed 547 times

1

In order to manipulate html tags in php I need to define a name for them, while javascript uses id, but I may have some kind of future problem if I use the same name I set for name in ids?
Ex..<input type="text" id="user" name="user">
if not, is this practice recommended or used? , or by convention usually different names are assigned?

  • No problem. But remember that the id can only be used once, cannot use the same value again, while the name can be used several times.

1 answer

3


Yes, you can use the same value for the attribute id and name in the same element without problems.

It is worth remembering that the value of id should be unique for each document, the value of the name may occur more than once per document.

[...] while javascript uses id [...]

Just one note: You can select, by javascript, elements by id (getElementById) or by name (getElementsByName):

 console.log("Elemento por ID:", document.getElementById("user"));
 console.log("Elemento por Name:", document.getElementsByName("user")[0]);
  <input type="text" id="user" name="user">

Browser other questions tagged

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