2

I am customising Sage CRM, so I have no control over the HTML that is written and can't add IDs or class's to the table layouts the CRM spits out. I want to hide a higher (not top) level table based on a users selection of a select dropdown. I can only get a jQuery selector hooked onto the title row of a table within the table I want to hide.

The DOM goes something like:

//Lots of other table structures above this in the DOM....
<table>  <---- this is the table I want to show or hide based on the users selection
  <tbody>
    <tr>
     <td>
       <table>
         <tbody>
           <tr>
             <td class="PANEREPEAT">  <---- this is the node I can get selector to
                 Valuation information
////

So I do the below client side javascript:

    var val_information_screen;

    $('.PANEREPEAT').filter(function () {
        //Find the valuation information screen
        return $(this).text() == 'Valuation information';
    }).each(function () { //iterate through all of these (there should only be one!)
        val_information_screen = $(this);
    });

    var sel_ofee_type = $('#ofee_type');
    if (sel_ofee_type.val() == '006') {
        val_information_screen.closest('table').parents("table:first").show();
    } else {
        val_information_screen.closest('table').parents("table:first").hide();
    }

It does work, it just is not particularly beautiful. The bit that I really detest is below. Is there a better way to traverse up the DOM using jQuery?

val_information_screen.closest('table').parents("table:first").show();
val_information_screen.closest('table').parents("table:first").hide();
MagicalArmchair
  • 911
  • 1
  • 12
  • 26
  • `.closest()` and `.parents()` (and `.parent()`) are how you traverse up the DOM. You could use `.closest("table").closest("table")` or `.parents("table").eq(1)` – Ian Aug 06 '13 at 16:30
  • @Ian you're right, basically, but note that `.closest()` will include the starting node itself as a match. That is, the closest `` from a `
    ` will be that table itself, so there'd have to be an intervening `.parent()` call. (I think.)
    – Pointy Aug 06 '13 at 16:33
  • @Pointy Oh snap! Good point. Yeah you're right. I usually do remember `.closest()` starts looking at the current element, but didn't even think of that for chained `.closest()` calls with the same selector! – Ian Aug 06 '13 at 16:35
  • @Pointy I just wish `.parents()` had a way to stop, instead of traversing the whole way up the tree. Do you know if `.parents("table:eq(1)")` would stop traversing? Or if it's basically the same as `.parents("table").eq(1)`? I'm not sure how to test that – Ian Aug 06 '13 at 16:36

2 Answers2

6

If you are sure that it has fixed structure, then you can use this,

$(td-selector).parents("table").eq(1).hide();

In your case,

val_information_screen.parents("table").eq(1).hide();
Optimus Prime
  • 6,817
  • 5
  • 32
  • 60
  • The DOM is fixed, and this provides a more elegant solution and still functions. Thanks for your help. Just to be clear, if I wanted to step one table further UP the DOM, would I change .eq(1) to .eq(2)? – MagicalArmchair Aug 07 '13 at 07:25
0

If your DOM (specifically starting from table you want to hide till the td you have as selector) is pretty much fixed, then the below selector can be used.

$('#element').parents('table').eq(1)
whyAto8
  • 1,660
  • 4
  • 30
  • 56