Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript dom traversing

1.getElementById()
 <div id="user">Get Element by ID</div>
 const userId = document.getElementById(user)
 console.log(userId)

/* Output
<div id="user">Get Element by ID</div>*/

<!-------->
2.getElementsByClassName() 

<div class="user">Get Element by Class Name (1)</div>
const userClass = document.getElementsByClassName('user')
The getElementsByClassName() returns an array-like object of the elements.
We can convert the array-like object to an actual JavaScript array with the Array.from()
method or the spread operator.
/* With the Spread Operator */
const userClass = [...document.getElementsByClassName('user')]


 
PREVIOUS NEXT
Tagged: #javascript #dom #traversing
ADD COMMENT
Topic
Name
6+2 =