Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery selecting columns in tables and updating values onClick with both th and td present in table
    primarykey
    data
    text
    <p>I am trying to update cost values in a separate div from an external XML file when a user clicks on a table column.</p> <p>My table structure is as follows:</p> <pre><code>&lt;table id="benefit" class="portletTable benefitsTable" summary="This table displays the level of of benefit you would receive."&gt; &lt;caption&gt;Table of benefits&lt;/caption&gt; &lt;thead&gt; &lt;tr&gt; &lt;th width="33%" scope="row" class="firstHead"&gt; Select your level of cover &lt;/th&gt; &lt;th scope="col"&gt; &lt;a href="#"&gt;Level 1&lt;/a&gt; &lt;/th&gt; &lt;th scope="col"&gt; &lt;a href="#"&gt;Level 2&lt;/a&gt; &lt;/th&gt; &lt;th scope="col"&gt; &lt;a href="#"&gt;Level 3&lt;/a&gt; &lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr class="price"&gt; &lt;th scope="row"&gt; Price &lt;/th&gt; &lt;td&gt; &amp;pound;5 &lt;/td&gt; &lt;td&gt; &amp;pound;10 &lt;/td&gt; &lt;td&gt; &amp;pound;20 &lt;/td&gt; &lt;/tr&gt; &lt;tr class="benefit"&gt; &lt;th scope="row"&gt; &lt;div&gt; &lt;h4&gt;&lt;a href="/cash-plan-quote/jsp/popUp.jsp" class="pop pop-console" target="_blank" title="Opens in a new window" rel="800, 600"&gt;Dental&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;Full cost of treatment, up to 100%&lt;/p&gt; &lt;a href="/cash-plan-quote/jsp/popUp.jsp" class="info pop pop-console" target="_blank" title="Opens in a new window" rel="800, 600"&gt;More information on this benefit&lt;/a&gt; &lt;/div&gt; &lt;/th&gt; &lt;td&gt; &lt;h4&gt;&amp;pound;50&lt;/h4&gt; &lt;p&gt;per year&lt;/p&gt; &lt;/td&gt; &lt;td&gt; &lt;h4&gt;&amp;pound;100&lt;/h4&gt; &lt;p&gt;per year&lt;/p&gt; &lt;/td&gt; &lt;td&gt; &lt;h4&gt;&amp;pound;150&lt;/h4&gt; &lt;p&gt;per year&lt;/p&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr class="benefit"&gt; &lt;th scope="row"&gt; &lt;div&gt; &lt;h4&gt;&lt;a href="/cash-plan-quote/jsp/popUp.jsp" class="pop pop-console" target="_blank" title="Opens in a new window" rel="800, 600"&gt;Helplines&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;Available with all levels&lt;/p&gt; &lt;a href="/cash-plan-quote/jsp/popUp.jsp" class="info pop pop-console" target="_blank" title="Opens in a new window" rel="800, 600"&gt;More information on this benefit&lt;/a&gt; &lt;/div&gt; &lt;/th&gt; &lt;td&gt; &lt;img src="/cash-plan-quote/common/images/icons/tick.png" width="19" height="16" alt="This benefit is included" /&gt; &lt;/td&gt; &lt;td&gt; &lt;img src="/cash-plan-quote/common/images/icons/cross.png" width="19" height="16" alt="This benefit is not included" /&gt; &lt;/td&gt; &lt;td&gt; &lt;img src="/cash-plan-quote/common/images/icons/tick.png" width="19" height="16" alt="This benefit is included" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>So when a user clicks on the Level 1 column the prices are updated in a separate cost panel division on the fly.</p> <p>Here is the jQuery I am using to achieve this:</p> <pre><code>// Retrieve XML info on column click $('#benefit tr *').click(function(){ var colIndex = $(this).parent().children().index ($(this)); if (colIndex != 0) { $.get('/cash-plan-quote/poc.xml', function(data){ $(data).find('level').each(function() { var $level = $(this); var $levelName = $level.attr('name'); if (colIndex == $levelName) { var coverLevel = '&lt;span&gt;Level ' + $level.attr('name') + ' benefits&lt;/span&gt;'; var $month = $level.find('month').text(); var monthCost = '&lt;h5&gt;&amp;pound;' + $month + '&lt;/h5&gt;'; var $week = $level.find('week').text(); var weekCost = '&lt;h6&gt;&amp;pound;' + $week + '&lt;/h6&gt;'; $('div.costPanel &gt; h2 &gt; span').replaceWith($(coverLevel)); $('div.costPanel &gt; div.costs &gt; h5').replaceWith($(monthCost)); $('div.costPanel &gt; div.costs &gt; h6').replaceWith($(weekCost)); }; }); }); return false; }; </code></pre> <p>The problem is occurring using the * selector as this indexes the elements inside the ths and tds as well when I only want the function restricted to clicking on either the th or td.</p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload