+ 4

What is the meaning of " id" and what does it do?

2nd Aug 2017, 8:07 PM
Emily
Emily - avatar
5 ответов
+ 2
but what is its use in JavaScript?
2nd Aug 2017, 8:13 PM
Emily
Emily - avatar
+ 3
id stands for identity and just like you and I are unique, so does this attribute make an element uniquely identifiable.
2nd Aug 2017, 8:09 PM
Ghauth Christians
Ghauth Christians - avatar
+ 3
It's useful when you have a lot of elements that are the same but you just want to manipulate a specific element. Example (HTML, Javascript): //Let's say you have the following elements <div id="Emily" class="Human"></div> <div id="Gavin" class="Human"></div> <div id="Maz" class="Human"></div> <div id="Burey" class="Human"></div> //And let's say you want to manipulate "Gavin". How will you select the div "Gavin" when there are many divs? You select "Gavin" div by identifying it with it's "id" var sexyPerson = document.getElementById("Gavin"); //And now you can manipulate me
2nd Aug 2017, 8:22 PM
Ghauth Christians
Ghauth Christians - avatar
+ 2
Lets say you have two div elements in your html: <div>This is text. </div> <div>This is text. </div> Let's say you wanted to change only the second div's text. Well, an easy way of targeting only the second div would be to give it an id. <div id='one'>This is text. </div> <div id='two'>This is text. </div> Then, to get only the second div in javascript, you could do something like the following: var x = document.getElementById('two'); x.html('This is new text!'); And you would get: <div id='one'>This is text. </div> <div icd='two'>This is new text!</div> Obviously this becomes even more useful the more elements you have.
2nd Aug 2017, 8:21 PM
Christian Barraza
Christian Barraza - avatar
0
@Emily you can't challenge me on a language I never start learning
7th Aug 2017, 8:46 PM
Shuaib Abdulbaki
Shuaib Abdulbaki - avatar