I'm a little confused on how to do this...
I have a table that has a series of rows, and within each cell, there are certain form elements. I'm trying to get the value from the "code" input field from the last row only, and am having trouble with the syntax...
Simplified table looks like this:
<table id="table1">
<tr><td><input type="hidden" name="code" value="XFT" /></td></tr>
<tr><td><input type="hidden" name="code" value="ETY" /></td></tr>
<tr><td><input type="hidden" name="code" value="DHQ" /></td></tr>
</table>
And, here's the jquery that doesn't work...
if($('#cont')) {
$("#cont').live('click', function(event) {
var tr = $('#wr-viewcarttable tr:last');
var itemcode = $(tr > 'input[name="code"]').val();
window.location = "/search?p="+itemcode;
});
}
Try this:
$('table#table1 tr:last input[name=code]').val();
Or, adjusted to your code:
$('#cont').live('click', function(event) {
var tr = $('#wr-viewcarttable tr:last');
var itemcode = tr.find('input[name=code]').val();
window.location = "/search?p="+itemcode;
});
You have two errors in your code, you have mismatched quotes in the $("#cont')
part and your input search is wrong. What you have now is:
$(tr > 'input[name="code"]').val();
As the >
is outside quotes, it is not a string, but a comparison operator, which now compares tr
to 'input[name="code"]'
. Comparison operators always return boolean values (true or false), so you're effectively doing this:
$(true).val();
Which doesn't make much sense. If you have a jQuery object, you can use the find
method to find any children of that object, or alternatively pass the element as context to the $()
function. So these two will work and are equal:
tr.find('input[name=code]').val();
$('input[name=code]', tr).val();
There's actually no reason to save the tr
to it's own variable in your case, as you can get the value in just one declaration, as shown above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With