Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding, selecting and looping through XML data with jQuery
    primarykey
    data
    text
    <p>I have an XML document thus:</p> <p> </p> <pre><code>&lt;tab id="1"&gt; &lt;name&gt;Individual&lt;/name&gt; &lt;coverLevel&gt; &lt;level id="1"&gt; &lt;month&gt;20&lt;/month&gt; &lt;week&gt;5&lt;/week&gt; &lt;/level&gt; &lt;level id="2"&gt; &lt;month&gt;40&lt;/month&gt; &lt;week&gt;10&lt;/week&gt; &lt;/level&gt; &lt;level id="3"&gt; &lt;month&gt;80&lt;/month&gt; &lt;week&gt;20&lt;/week&gt; &lt;/level&gt; &lt;/coverLevel&gt; &lt;/tab&gt; &lt;tab id="2"&gt; &lt;name&gt;Couple&lt;/name&gt; &lt;coverLevel&gt; &lt;level id="1"&gt; &lt;month&gt;40&lt;/month&gt; &lt;week&gt;10&lt;/week&gt; &lt;/level&gt; &lt;level id="2"&gt; &lt;month&gt;80&lt;/month&gt; &lt;week&gt;20&lt;/week&gt; &lt;/level&gt; &lt;level id="3"&gt; &lt;month&gt;160&lt;/month&gt; &lt;week&gt;40&lt;/week&gt; &lt;/level&gt; &lt;/coverLevel&gt; &lt;/tab&gt; &lt;tab id="3"&gt; &lt;name&gt;Family&lt;/name&gt; &lt;coverLevel&gt; &lt;level id="1"&gt; &lt;month&gt;80&lt;/month&gt; &lt;week&gt;20&lt;/week&gt; &lt;/level&gt; &lt;level id="2"&gt; &lt;month&gt;160&lt;/month&gt; &lt;week&gt;40&lt;/week&gt; &lt;/level&gt; &lt;level id="3"&gt; &lt;month&gt;320&lt;/month&gt; &lt;week&gt;80&lt;/week&gt; &lt;/level&gt; &lt;/coverLevel&gt; &lt;/tab&gt; &lt;tab id="4"&gt; &lt;name&gt;Single parent family&lt;/name&gt; &lt;coverLevel&gt; &lt;level id="1"&gt; &lt;month&gt;40&lt;/month&gt; &lt;week&gt;10&lt;/week&gt; &lt;/level&gt; &lt;level id="2"&gt; &lt;month&gt;80&lt;/month&gt; &lt;week&gt;20&lt;/week&gt; &lt;/level&gt; &lt;level id="3"&gt; &lt;month&gt;160&lt;/month&gt; &lt;week&gt;40&lt;/week&gt; &lt;/level&gt; &lt;/coverLevel&gt; &lt;/tab&gt; </code></pre> <p></p> <p>And jQuery which calls said XML document and dynamically updates values onClick of a table column. </p> <pre><code>$(document).ready(function(){ $('table#benefit &gt; thead &gt; tr &gt; th, table#benefit &gt; thead &gt; tr &gt; th &gt; a, table#benefit &gt; tbody &gt; tr &gt; td').click(function(){ var colIndex = $(this).parent().children().index ($(this)); var tabPosition = $('ul#coverTabs &gt; li').index ($('.currentTab')); var tabPosition = tabPosition + 1 if (colIndex != 0) { $.get('/cash-plan-quote/table.xml', function(data){ $(data).find('level').each(function() { var $level = $(this); var $levelID = $level.attr('id'); if (colIndex == $levelID) { var coverLevel = '&lt;span&gt;Level ' + $levelID + ' benefits&lt;/span&gt;'; var monthCost = '&lt;h5&gt;&amp;pound;' + $level.find('month').text() + '&lt;/h5&gt;'; var weekCost = '&lt;h6&gt;&amp;pound;' + $level.find('week').text() + '&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>What i would like to do is retrieve the data in the XML doc for the current tab:</p> <pre><code> var tabPosition = $('ul#coverTabs &gt; li').index ($('.currentTab')); var tabPosition = tabPosition + 1 </code></pre> <p>So when a user clicks on a tab the XML level values of the tab will be called. I thought i could do this by finding the position of the tab and then use that to retrieve the data for that tab in the XML doc.</p> <p>The HTML for the tabs:</p> <pre><code>&lt;ul id="coverTabs"&gt; &lt;li class="currentTab"&gt;&lt;a href="#"&gt;Individual&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Couple&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Family&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;Single parent family&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>And some more jQuery to set the styling of the current tab:</p> <pre><code>$(".currentTab").removeClass("currentTab"); $(this).addClass("currentTab"); </code></pre>
    singulars
    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.
    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