Results 1 to 5 of 5

Thread: How to get inner div ids from outer div id

  1. #1
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to get inner div ids from outer div id

    I am searching for the solution but didn't get any!
    so i decided to post it so here's the code. the inner div's ids are generated dynamically, so we didn't know what are the ids . and thats the problem how to get those ids.
    heres the example code:

    <div id = "parent_id">
    <div id = "inner_div_19"></div>
    <div id = "inner_div_210"></div>
    <div id = "inner_div_11"></div>
    <div id = "inner_div_99"></div>
    </div>

    How to get inner div ids from the parent div id.

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    function Test(id){
     var par=document.getElementById(id);
     var clds=par.childNodes;
     for (var z0=0;z0<clds.length;z0++){
      if (clds[z0].nodeName=='DIV'){
       alert(clds[z0].id);
      }
     }
    
    
    }
    /*]]>*/
    </script></head>
    
    <body>
    <div id = "parent_id">
    <div id = "inner_div_19"></div>
    <div id = "inner_div_210"></div>
    <div id = "inner_div_11"></div>
    <div id = "inner_div_99"></div>
    </div>
    <input type="button" name="" value="Test" onclick="Test('parent_id');"/>
    </body>
    
    </html>

  3. The Following User Says Thank You to vwphillips For This Useful Post:

    jawainc (12-29-2008)

  4. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    XHTML should almost never be used. The language attribute for the script tag has been deprecated. The childNodes need not be used* since a more convenient object exists - getElementsByTagName('div'):

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function getIds(par){
     par = document.getElementById(par).getElementsByTagName('div');
     for(var a = [], i = 0; i < par.length; ++i)
      a.push(par[i].id || 'has no ID');
     alert(a.join('\n'));
    };
    </script>
    </head>
    <body>
    <div>
    <div id = "parent_id">
    <div id = "inner_div_19"></div>
    <div id = "inner_div_210"></div>
    <div id = "inner_div_11"></div>
    <div id = "inner_div_99"></div>
    </div>
    <input type="button" onclick="getIds('parent_id');" value="Get Id's">
    </div>
    </body>
    </html>
    * The childNodes will not pick up nested divisions within the nested divisions though, so depending upon how deep one wants to probe, may be preferred.
    Last edited by jscheuer1; 12-24-2008 at 02:20 PM. Reason: add * comment about childNodes
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #4
    Join Date
    Dec 2008
    Location
    Nigeria
    Posts
    95
    Thanks
    3
    Thanked 8 Times in 8 Posts

    Default

    Check out this correction, and compare it with the one u made before!
    <html>
    <head>
    <title>Fixed Already</title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/

    function Test(id){
    var pars=document.getElementById(id).getElementsByTagName("DIV");
    for (var z=0;z<pars.length;z++){
    alert(pars[z].id);
    }
    }
    /*]]>*/
    </script></head>

    <body>
    <div id = "parent_id">
    <div id = "inner_div_19"></div>
    <div id = "inner_div_210"></div>
    <div id = "inner_div_11"></div>
    <div id = "inner_div_99"></div>
    </div>
    <input type="button" name="" value="Test" onclick="Test('parent_id');"/>
    </body>
    </html>

  6. #5
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    thanks guys you help me alot. Problem Solved

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •