views:

42

answers:

2

This is driving me nuts...I am trying to iterate through rows in a table. I know the classname for the tbody. Can I just specify the class for the tbody and iterate through the rows? For example...

<div class="dclass1" ...>
   <table summary="" style="margin-top: 0pt;">
     <thead ...>
     <tbody class="tbClass1" ..>
      ...
     <tbody class="tbClass2" ..>
       <tr ..> 
          ...
       <tr ...>
     </tbody>
...

How do I loop through (or just specify the selector) for the rows in class="tbClass2"?

+4  A: 

Ok you added the additional information that the data you want to access is in an iframe. As I already explained the following code will only work if the iframe content resides on the same domain as the page it is embedded in.

$(document).ready(function() {
    var iframe = $("iframe#myiframe");
    //wait for iframe to load
    $(iframe).load(function() {
        iframe = iframe.get(0);
        //handle browser specifics
        var oDoc = (iframe.contentWindow || iframe.contentDocument);
        if (oDoc.document)
            oDoc = oDoc.document;
        $("tbody.tbClass2 > tr", oDoc).each(function(index, element) {
            alert(index); //or do whatever
        });
    });
});

Demo page http://jsbin.com/afumi which includes http://jsbin.com/aqaxu via iframe.

As already stated this won't work if iframe points to remote content as then browsers will deny access to the iframes DOM

e.g.

<iframe src="http://www.google.com"&gt;...&lt;/iframe&gt;


$("tbody.tbClass2 > tr")

Now you could do

$("tbody.tbClass2 > tr").each(function(index, element) {
    //do whatever iterating over all TRs
});
jitter
See the above comment..is the iframe causing the problem?
GregH
A: 

Try:

$(".tbClass2").children("tr");

You can then iterate with eg:

$(".tbClass2").children("tr").each( function() {
  // do something here
});
richsage
Doesn't seem to work. Could the fact that these elements are in an iframe be causing it not to be able to select those objects?
GregH
Wow! Of course it matters if an iframe comes into play. That changes the whole thing. Is the javascript also running in the iframe itself? If not the iframe has at least to point (src) to something on the same domain else the browser denies access to iframe content.
jitter
@GregH: you should add that tidbit of VERY IMPORTANT INFORMATION to the question.
Crescent Fresh