Javascript Reference Guide

 
 
This guide contains useful Javascript tips.

rss feed






Find div with certain class.



Information
On this web page find all divs with the class "FindMe". When these specific div's are found, display the text.

Text 1.
Text 2.


Code
    <div class="FindMe">Text 1.</div>
    <div class="FindMe">Text 2.</div>
 
    <script type="text/javascript" language="JavaScript">
    function find_div_class() {
        var divCollection = document.getElementsByTagName("div");
        for (var i=0; i<divCollection.length; i++) {
            if(divCollection[i].getAttribute("class") == "FindMe") {
                findMeText = divCollection[i].innerHTML;
                alert(findMeText);
            } 
        }
    }
    </script>
    
    Press the button to find the divs with class "FindMe".<br />
    <input type="button" onfocus="this.blur()" 
    value="FindMe" onclick="javascript:find_div_class();return false;""/>  
 
Result

Press the button to find the divs with class "FindMe".


This solution works for Firefox 3.5.x, Safari 4.0.x and Google Chrome 5.0.x but not for IE 8.