I am not sure of the functions you have used, 'index' and 'closest', I
have not seen them, atleast it is not part of the cheat sheet I use.

ok, the following code could solve your problem,

//1) get the quantity above the price that is clicked
var tableDom = $(this).parents('table')[0];
var quant = tableDom.rows[0].cells[this.cellIndex].innerHTML;
console.log(quant);
//2) get the content of the heading above the relevant table
var h2heading = $(this).parents('table').prevAll('h2').eq(0).text();
console.log(h2heading);

you can also address issue 1 by using the data() function that is
available in jquery,
 but you would have to loop through your data to spit out the jquery
that will do this.

hope this helps
On Feb 5, 6:19 am, paulswansea <sendtoswan...@hotmail.com> wrote:
> Hi,
>
> I have a page with multiple tables, and need to get the index value of
> a cell with a price which i click on, so i can extract the relevent
> quantity within the row above, i've tried using the index() function
> but to no avail!
>
> also, I wish to extract the content of the previous h2 header, i'm not
> sure if closest('h2') is the best way to do it, but it also doesn't
> work, could someone please tell me how to do this?
>
> Please see below for the code (note that there is roughly about ten
> tables to which i've cut down to two for here, also I CANNOT change
> the tables i.e. add id or classes anywhere, so the tables have to stay
> the way they are, I just need to get some jquery code to work around
> this and extract the relevant data).
>
> So a brief run down of what i need to do :
>
> 1) get the quantity above the price that is clicked
> 2) get the content of the heading above the relevant table
>
> THE CODE :
>
> <h2 class="sublisting">First Item Named here</h2>
> <p>information goes here</p>
> <table class="price_table">
>   <tr>
>     <th> Quantity</th>
>     <th> 1000 </th>
>     <th> 5000 </th>
>     <th> 10,000 </th>
>     <th> 15,000 </th>
>     <th> 20,000 </th>
>   </tr>
>   <tr>
>     <td> Price </td>
>     <td><strong>£400</strong><br />
>       <span class="each_item">40p each</span></td>
>     <td><strong>£1650</strong><br />
>       <span class="each_item">33p each</span></td>
>     <td><strong>£3200</strong><br />
>       <span class="each_item">32p each</span></td>
>     <td><strong>£4800</strong><br />
>       <span class="each_item">32p each</span></td>
>     <td><strong>£6200</strong><br />
>       <span class="each_item">31p each</span></td>
>   </tr>
> </table>
> <h2 class="sublisting">second Item Named here</h2>
> <p>information goes here</p>
> <table class="price_table">
>   <tr>
>     <th> Quantity</th>
>     <th> 1000 </th>
>     <th> 5000 </th>
>     <th> 10,000 </th>
>     <th> 15,000 </th>
>     <th> 20,000 </th>
>   </tr>
>   <tr>
>     <td> Price </td>
>     <td><strong>£400</strong><br />
>       <span class="each_item">40p each</span></td>
>     <td><strong>£1650</strong><br />
>       <span class="each_item">33p each</span></td>
>     <td><strong>£3200</strong><br />
>       <span class="each_item">32p each</span></td>
>     <td><strong>£4800</strong><br />
>       <span class="each_item">32p each</span></td>
>     <td><strong>£6200</strong><br />
>       <span class="each_item">31p each</span></td>
>   </tr>
> </table>
> <script type="text/javascript">
> //<![CDATA[
>   $('.each_item').parent().click( function() {
>         var indexvar = $(this).parent().index(this); /********* NOT WORKING
> ************/
>         var headerval = $(this).closest('h2').text(); /********** ALSO NOT
> WORKING **********/
>         var quantity = $(tbodyvar).children('tr').children('th').eq
> (indexvar).text();
>         alert('Requested ' + quantity + ' of ' + headerval);
>   });
>
> //]]>
> </script>

Reply via email to