getElementsByTagName()
is a method that returns an array filled with all the elements in the document that have the specified tag name sent as a argument.
Syntax:
document.getElementsByTagName('div')- refers to all DIV tags.
length
property:
var divs = document.getElementsByTagName('div'); var nr_divs = divs.length;
The array returned by getElementsByTagName()
can be parsed with a for()
instruction. To its items you can apply properties and methods specific to HTML objects in JavaScript.
See the list from the page: coursesweb.net/javascript/properties-methods-html-elements
getElementsByTagName()
method. As a general syntax, it can be used the following code:
var elms = document.getElementsByTagName('tagName'); for (var i=0; i<elms.length; i++){ elms[i].property;- 'elms[i].property' apply the JS 'property' to all 'elms' (tag elements).
}
<h4>Example getElementsByTagName</h4> <p>Content with strings enclosed in SPAN tags:<br> coursesweb.net : <span>Free</span> web JavaScript <span>Courses</span> and <span>tutorials</span>. </p> <p>- Click on the button to set CSS properties: 'color' and 'text-decoration' to all SPAN tags in page.</p> <button id='btn1'>Style Span</button> <script> function adStyle(tag){ //array with all the tags specified in 'tag' parameter var elms = document.getElementsByTagName(tag); //traverse the elms array for(var i=0; i<elms.length; i++){ //apply style properties elms[i].style.color ='blue'; elms[i].style.textDecoration ='underline'; } } //when click on #btn1 it calls adStyle() with 'span' argument document.getElementById('btn1').addEventListener('click', (ev)=>{ adStyle('span'); }); </script>
document.getElementsByTagName('tagName')
it gets a reference to all 'tagName' in the HTML document. There are cases when you want to get only the tags within another tag. In this case, add an ID to that HTML element. In the JS script you can use getElementById()
to get an object of that element, than, you can apply getElementsByTagName()
to that object.<h4>Example getElementsByTagName() after getElementBiId()</h4> <ul id='ul1'> <li>ul1 - The next one comes.</li> <li>ul1 - I came for nothing.</li> </ul> <ul id='ul2'> <li>ul2 - I am the one who comes,</li> <li>ul2 - To me.</li> </ul> <p>- If you click the button, it apply the CSS properties: 'color' and 'text-decoration' to LI tags in element with id 'ul2'.</p> <button id='btn1'>Style Li</button> <script> function adStyle(id, tag){ //array with specified 'tag' in the elementul with id from 'id var elms = document.getElementById(id).getElementsByTagName(tag); //traverses the array elms for(var i=0; i<elms.length; i++){ //aplica proprietati de stil elms[i].style.color ='blue'; elms[i].style.textDecoration ='underline'; } } //when click on #btn1 it calls the adStyle() with the arguments for id and tag document.getElementById('btn1').addEventListener('click', (ev)=>{ adStyle('ul2', 'li'); }); </script>
getAttribute('attr_name')
method inside the for()
instruction used to traverse the Array returned by getElementsByTagName()
.var elms = document.getElementsByTagName('tag'); for(var i=0; i<elms.length; i++){ if (elms[i].getAttribute('attr_name')=='val'){ // JS code ... } }- val is the value of the attribute from 'attr_name' in the specified 'tag'.
<h4>Example getElementsByTagName() and getAttribute()</h4> <p>Content with texts in SPAN tags:<br> <span data-bgr='yellow'>Free</span> web development <span data-bgr='yellow'>course</span> and <span data-bgr='green'>tutorials</span>.</p> <p>- If you click on the button, it sets a background color to the SPAN tags that have 'data-bgr' attribute with 'yellow' value.</p> <button id='btn1'>Add Bgr</button> <script> function adBgr(tag){ //array with span 'tag' var elms = document.getElementsByTagName(tag); //traverses the elms for(var i=0; i<elms.length; i++){ //checks the data-bgr attribute if(elms[i].getAttribute('data-bgr') =='yellow'){ elms[i].style.background ='yellow'; } } } //when click on #btn1 calls adBgr() function document.getElementById('btn1').addEventListener('click', (ev)=>{ adBgr('span'); }); </script>
DOMContentLoaded
event to the document
object.<h4>Example DOMContentLoaded</h4> <p>- After the HTML document is loaded, it gets an Input element and the LI tags, and registers 'click' event to those objects.<br> If you click on any of the lists below, it adds its text in the input field.</p> Input Text: <input type='text' id='inp1' readonly/> <ul> <li>Peace is Good.</li> <li>Forgiving brings happiness.</li> <li>Love's Divine</li> </ul> <script> var inp1; //it will contains the #inp1 element //function called with DOMContentLoaded function liClick(){ inp1 = document.getElementById('inp1'); // #inp1 text field //array with LI tags var elms = document.getElementsByTagName('li'); //traverses elms and registers click to each item for(var i=0; i<elms.length; i++){ elms[i].addEventListener('click', (ev)=>{ inp1.value = ev.target.textContent; }); } } //after the HTML is loaded in DOM, calls liClick() document.addEventListener('DOMContentLoaded', liClick); </script>
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net