Reputation: 55
I have a code:
<table>
<tr>
<td>
<input type="text" value="1" />
<input id="button" type="button" value="Click me!" />
</td>
</tr>
</table>
I want to get value of input[type="text"]
by below code but It doesn't work, please help me
<script type="text/javascript">
$(document).ready(function() {
$("#button").bind("click", function() {
alert($(this).siblings().find('input[type="hidden"]').val(););
});
});
</script>
Upvotes: 0
Views: 257
Reputation: 34489
Well the first problem is that you're trying to find hidden inputs, rather than text based inputs. You also need to move your filter into the siblings call to make it work correctly, although you did have an extra semi-colon too:
$(document).ready(function() {
$("#button").bind("click", function() {
alert($(this).siblings('input[type="text"]').val());
});
});
See JSFiddle
Upvotes: 2
Reputation: 6180
Try this.
<script type="text/javascript">
$(document).ready(function() {
$("#button").bind("click", function() {
alert($(this).parent().find('input[type=text]').val());
});
});
</script>
Upvotes: 1
Reputation: 15387
First of all, Your input type is text
not the hidden
. You are fetching data using hidden
in Jquery then use as below if this is hidden:
$(document).ready(function() {
$("#button").bind("click", function() {
alert($(this).siblings('input[type="hidden"]').val());
});
});
if this is text:
$(document).ready(function() {
$("#button").bind("click", function() {
alert($(this).siblings('input[type="text"]').val());
});
});
Upvotes: 1
Reputation: 6111
Try this.
$("#button").click(function() {
alert($(this).prev('input[type="text"]').val());
});
Demo Here
Upvotes: 2
Reputation: 32581
.find()
searches for children, so you cant to use it here. Just use siblings()
$(this).siblings('input').val()
Upvotes: 3
Reputation: 148120
You need to find in siblings not in descendants of siblings.
alert($(this).siblings('input[type="hidden"]').val());
Upvotes: 2
Reputation: 2820
change
alert($(this).siblings().find('input[type="hidden"]').val());
to
alert($(this).siblings().find('input[type="text"]').val());
Upvotes: 0
Reputation: 3170
use jQuery prev()
as in alert($(this).prev().val());
It gets the immediately preceding sibling.
Upvotes: 1