0

everyone i want get all the p elements with class "a1" under div with class "oshow" which is agian under class "pro_line" without using getelementsbyclassname in javascipt

 <div id="Zi03">
    <div class="ye">text</div>
    <div class="yee">text</div>
    <div class="pro-line">
         <div class="oshow">
         <div class="compic"><a href="#"></a></div>
         <p class="a1"><a href="poduct"></a></p>
         <p class="a2"><a href="poduct"></a></p>
         <div class="a3"><a href="poduct"></a></div>
        </div> 
         <div class="oshow">
         <div class="compic"><a href="#"></a></div>
         <p class="a1"><a href="poduct"></a></p>
         <p class="a2"><a href="poduct"></a></p>
         <div class="a3"><a href="poduct"></a></div>
        </div>
         <div class="oshow">
         <div class="compic"><a href="#"></a></div>
         <p class="a1"><a href="poduct"></a></p>
         <p class="a2"><a href="poduct"></a></p>
         <div class="a3"><a href="poduct"></a></div>
        </div> ...
        ....
        ....
    </div>
 </div>

Please somebody help me how to get it in javascript without using getelementsbyclassname but i can use it by ID

Thanks alot in advance....

Chethu
  • 555
  • 4
  • 13

3 Answers3

1

You can use following replacement for getElementsByClassName() of modern browsers:

function getElementsByClassName(node, classname) {
        /// <summary>
        /// Replaces built-in function of modern browser. Implemented because not supported by IE7/8
        /// </summary>
        /// <param name="node">DOM element withing which search is done</param>
        /// <param name="classname">String with class name for search</param>
        /// <returns type="">Array of found elements</returns>

        var a = [];
        var re = new RegExp('(^| )' + classname + '( |$)');
        var els = node.getElementsByTagName("*");
        for (var i = 0, j = els.length; i < j; i++)
            if (re.test(els[i].className)) a.push(els[i]);
        return a;
    }

Call it with something like:

var aElems = getElementsByClassName(document.getElementById('Zi03'),'pro-line')
Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
  • This doesn't make sure the you only get `.a1` under `oshow` under `pro-line` You need to call getElementsByClassName multiple times to make sure the hierarchy restrictions are followed – Ruan Mendes Jun 07 '13 at 18:29
  • That's implicit. Of course call will have to be repeated as needed. – Yuriy Galanter Jun 07 '13 at 18:33
  • I disagree that it's implicit, it's easy to be tempted and just write `getElementsByClassName(document.getElementById('Zi03'),'a1')`. It looks like half a solution.I think it's still misleading since it doesn't really answer the question fully – Ruan Mendes Jun 07 '13 at 19:56
0
var elem = document.getElementById('Zi03');
var proline = elem.children[2];

For loop for all immediate childs of proline 
   var oshow= proline.firstElementChild;
   var a1 = show.children[1]
   //display a1 Or whatever you want
End 

This works if a1 and other elements are at same hierarchy level

Ani
  • 4,473
  • 4
  • 26
  • 31
0

Here's a version to get you started, it's not perfect but you can improve it

var nodes = [];
var allChildNodes = document.getElementById('Zi03').getElementsByTagName('*');
for  (var i = 0; i < allChildNodes.length); i++ ) {
    var isA1 = ;
    var isUnderProline =  allChildNodes[i].parentNode.className == 'pro-line';
    var isUnderOShow = allChildNodes[i].parentNode.parentNode.className == 'oshow';

    if (
            allChildNodes[i].className == 'a1' &&
            allChildNodes[i].parentNode.className == 'pro-line' &&
            allChildNodes[i].parentNode.parentNode.className == 'oshow'  

       ){
       nodes.push(allChildNodes[i]);
    }
}
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217