I am trying to write some Jquery code that will get the value of the next textbox that is of a class “in”. The code I have here isn’t working, the value is always undefined. I think I am using the next() function incorrectly but I am not sure, does anybody have any input?
<!DOCTYPE html>
<html>
<body>
<input type="text" class="mm" />
<br>
<input type="text" class="in" />
$(document).ready(function() {
$("input").focus(function() {
var value = $(this).next(".in").val();
if (value) {
alert(value);
}
});
});
</script>
</body>
</html>
Your example as you’ve given the HTML actually works. You can see it here: http://jsfiddle.net/jfriend00/RGsDq/. Based on your comments, it sounds like you want the first sibling that has the class “in” whether it’s the next sibling or not. .next() only examines the very next sibling. If you want the first sibling that has the class “in” even if it’s not the very next sibling, you can do either of these:
You can see the first of these two options working here: http://jsfiddle.net/jfriend00/4VwAN/