views:

305

answers:

2

Hey all,

The title on this one may be a bit misleading as it's not as simple as it sounds. I've got a page with two tables (each has 50 rows) shown side-by-side so that the rows align with one another. The problem I'm facing is that the default height for a row in each table is the same but occaisionally dynamic content in the second table causes a specific row to be taller than the others.

"Not a problem!", I said to myself. "I'll just get the height of the row in the second table when the page loads and then set the height of the same row in the first table to match it!" Therein lies my problem. I can't seem to get the numbers of rows in each table to match up progamatically. Without that, I'm not sure how to change the height of the correct row in the first table. I think it may be due to the content of the cells in the second table. Many of the cells actually contain their own tables (and table rows) and so my jQuery selector is getting all the rows, not just the parent ones.

Here's a quick sample of the markup I'm being forced to work with:

<div id="mainTable">
  <div id="randomServerGeneratedNumbers" rel="MainTable">
    <htmldb:randomNumbers>
      <table id="moreRandomNumbers">
        <tbody>
          <tr></tr> <!--THESE ARE THE ROWS I WANT!!!!-->
          <tr>
            <td>
              <table>
                <tr></tr> <!--IGNORE THESE!!!-->
              </table>
            </td>
          </tr>
          <tr></tr>
        </tbody>
        <tfoot></tfoot>
      </table>
    </htmldb>
  </div>
</div>

And here's my jQuery selector that almost working:

$('#mainTable').find('div[rel=mainTable] tbody tr:gt(0)');
+1  A: 

You want to use the child selector: A > B. The following jQuery code will select all the immediate child rows of the <tbody /> element within the first table found within <div rel="MainTable" />:

$('div[rel=MainTable] table:first > tbody > tr')

See the jQuery documentation for more.

Annabelle
Now the size of the jQuery object is 0 instead of 747. It should be around 50, so I'm afraid this isn't correct.
Sonny Boy
It looks correct to me. Assuming that `moreRandomNumbers` is actually a random set of numbers, your selector can't be identical to what Douglas posted. What are you actually using, that isn't working?
Joel Mueller
Check generated HTML source, maybe that `<htmldb>` thing is doing something with `id="moreRandomNumbers"`.
BalusC
If it's true that the outer table has an ID, but you don't know what it is, and the nested tables do not have id's, maybe this would work: `$('#mainTable').find('div[rel=mainTable] table[id] > tbody > tr');`
Joel Mueller
What exactly did you try? I edited it a couple of times; I know the first form was broken.
Annabelle
And yes, my answer assumes 'moreRandomNumbers' is actually the ID, and not just a standin for a random ID...
Annabelle
If `<table id="moreRandomNumbers" />` will always be the first table beneath `<div rel="MainTable" />`, then you can use the `:first` selector as in my updated answer.
Annabelle
A: 

I'd give the parent table rows a specific class. And then you can access them by the class name:

<div id="mainTable">
<div id="randomServerGeneratedNumbers" rel="MainTable">
<htmldb:randomNumbers>
  <table id="moreRandomNumbers">
    <tbody>
      <tr class="row"></tr> <!--THESE ARE THE ROWS I WANT!!!!-->
      <tr class="row">
        <td>
          <table>
            <tr></tr> <!--IGNORE THESE!!!-->
          </table>
        </td>
      </tr>
      <tr class="row"></tr>
     </tbody>
     <tfoot></tfoot>
     </table>
     </htmldb>
  </div>

and select like:

$('table#moreRandomNumbers .row');
munch
The tables (along with their random number IDs and their rows) are dynamically generated by a process over which I have no control. I'm afraid I can't use this solution.
Sonny Boy