[jQuery] Re: Having problems getting index() and closest() to work

2009-02-05 Thread Ricardo Tomasi

duplicate:
http://groups.google.com/group/jquery-en/browse_thread/thread/1b8e73583661fa52/ec7d0d275d484f66?hl=en#ec7d0d275d484f66

On Feb 5, 9:19 am, paulswansea  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 :
>
> First Item Named here
> information goes here
> 
>   
>      Quantity
>      1000 
>      5000 
>      10,000 
>      15,000 
>      20,000 
>   
>   
>      Price 
>     £400
>       40p each
>     £1650
>       33p each
>     £3200
>       32p each
>     £4800
>       32p each
>     £6200
>       31p each
>   
> 
> second Item Named here
> information goes here
> 
>   
>      Quantity
>      1000 
>      5000 
>      10,000 
>      15,000 
>      20,000 
>   
>   
>      Price 
>     £400
>       40p each
>     £1650
>       33p each
>     £3200
>       32p each
>     £4800
>       32p each
>     £6200
>       31p each
>   
> 
> 
> //   $('.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);
>   });
>
> //]]>
> 


[jQuery] Re: Having problems getting index() and closest() to work

2009-02-05 Thread ksun


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  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 :
>
> First Item Named here
> information goes here
> 
>   
>      Quantity
>      1000 
>      5000 
>      10,000 
>      15,000 
>      20,000 
>   
>   
>      Price 
>     £400
>       40p each
>     £1650
>       33p each
>     £3200
>       32p each
>     £4800
>       32p each
>     £6200
>       31p each
>   
> 
> second Item Named here
> information goes here
> 
>   
>      Quantity
>      1000 
>      5000 
>      10,000 
>      15,000 
>      20,000 
>   
>   
>      Price 
>     £400
>       40p each
>     £1650
>       33p each
>     £3200
>       32p each
>     £4800
>       32p each
>     £6200
>       31p each
>   
> 
> 
> //   $('.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);
>   });
>
> //]]>
>