views:

93

answers:

4

please tell me how can i get array of all Those DIV of specific table whose LANG="1" using javascript

table structure is like this:

  < table >
 < tr > < td > < div id=1 lang="1" > some metter < /div > < /td >< /tr >
 < tr > < td > < div id=2 lang="2" > some metter < /div > < /td >< /tr >
 < tr > < td > < div id=3 lang="1" > some metter < /div > < /td >< /tr >
 < tr > < td > < div id=4 lang="1" > some metter < /div > < /td >< /tr >
< /table >
+1  A: 

This is easiest if you use a library like Prototype, jQuery, Closure, etc., which offer a nearly-full set of CSS3 selectors.

Using Prototype, it would look like this:

var table = /* ...an expression finding the table...*/;
var divs = table.select('div[lang=1]');

So for instance, if you give your table an id of "myTable":

var table = $('myTable');
var divs = table.select('div[lang=1]');

Or just

var divs = $('myTable').select('div[lang=1]');

Using jQuery, if you give that id to your table, it's:

var divs = $('#myTable div[lang=1]');

(Yes, jQuery and Prototype both use $, but for different things.)

T.J. Crowder
giving me error: table.select is not a function (here table is var table "
Rajesh Rolen- DotNet Developer
+1  A: 

Try

$('table[lang="1"]');

or assign them class lets say "divclass" and select them

$('.divclass[lang="1"]');

This is jQuery syntax since someone else posted in prototype

c0mrade
That gives him *all* divs in *all* tables, and doesn't filter by `lang` as requested.
T.J. Crowder
@T.J. Crowder Sorry I didn't see that requirement here I'll update my answer
c0mrade
+4  A: 

You could give the table a unique id and then use the getElementsByTagName function:

var table = document.getElementById('tableId');
if (table == null) return;
var divs = table.getElementsByTagName('div');
for (var i = 0; i < divs.length; i++) {
    var div = divs[i];
    if (div.lang == '1') {
        alert(div);
    }
}
Darin Dimitrov
+1 for no "cool jslib" dependencies
markcial
Thanks a lot dear... all most everybuddy has given correct answer but i felt ur answer really very simple and very good...
Rajesh Rolen- DotNet Developer
+3  A: 

what happend with ol' plain javascript?

var divs = document.getElementsByTagName('table')[0].getElementsByTagName('div');

edit : forgot last part

var divLang = []; 
for(a in divs){
  divs[a].getAttribute('lang')==1?
    divLang.push(divs[a]):
    false;
}
markcial