Test

Lorem Ipsum.

Javascript selectors

How to select DOM elements in javascript?
Such as:

<div class="des"> <h1>Test</h1> <div class="desleft"> <p>Lorem Ipsum.</p> </div> <div class="Right"> <button>Test</button> </div> </div> 

Now how to choose h1 ? This is just part of a larger page, so getElementsByTagName() cannot be used, as others can be selected. Also, since there may be another h1 in the document later, I cannot attach the index (body) to the above.

Is there an easy way to select, say, the <h1> , which is under the desleft class desleft ? I can not use jQuery or any other libraries.

+8
javascript dom selector traversal
source share
6 answers
 getElementsByTag() 

There will be a function that you can start with, and then you can filter for DOMElements that have a class.

 var h1_array = document.getElementsByTag('h1'); var h1_class_array = []; for (var i=0, len=h1_array.length; i < len; i++) { if (h1_array[i].className.indexOf('classname') !== -1) { h1_class_array.push(h1_array[i]); } } 

The .indexOf function returns -1 if the needle is not found in the haystack.

Now re-read your question, why not just provide your h1 id?

DOM bypass is one of the javascript related issues (type jQuery).

a simple getElementById() will save you a headache, and the identifiers on all your h1s will be much cleaner at the end than trying to formulate an algorithm for choosing them in other ways.

+3
source share

You can use this to get to your H1:

 var des = document.getElementsByClassName('des') var fc = des[0].getElementsByTagName('h1') alert(fc[0].innerHTML) 
+8
source share

w3.org now has a selector (http://www.w3.org/TR/selectors-api/#examples). Here are two different ways that worked for me in Chrome. You can use the querySelectorAll function, which returns a list.

 <script type="text/javascript"> //looks for <h1> tag under <div> with className "des" showOff1 = function() { var x = document.querySelector(".des h1"); alert(x.innerHTML); } //looks for <div> tag with className "desleft" and then use previousSibling to traceback <h1> tag showOff2 = function() { var y = document.querySelector("div.desleft"); var z = y.previousSibling.previousSibling; alert(z.innerHTML); } </script> <body onload="showOff2();"> 
+7
source share

Use querySelectorAll

You can use querySelectorAll :

 // Will return a NodeList even if there is only one element found var heading = document.querySelectorAll('.des > h1'); heading[1].style.color = 'red'; // NodeList is similar to an array 

This will return a NodeList .

or

Use querySelector to return the first element found:

 var first_heading = document.querySelector('.des > h1'); first_heading.style.color = 'blue'; 

Commonly used with the id #single-header-id selector.

Here is a demo

+7
source share

If you want to select h1 , which is before the first element of the desleft class, you can always do this:

 document.getElementsByClassName("desleft")[0].previousSibling.previousSibling 

Example: http://jsfiddle.net/Xeon06/ZMJJk/

previousSibling needs to be called twice due to the empty node text between them. That's why using libraries for this is really the best way to go.

+1
source share
 var h1 = document.querySelector('.desleft').previousElementSibling; 
  • Find an element with class Name = 'desleft' using the '.desleft' selector
  • Just go back to the previous element (not to the previous node!)
0
source share

All Articles