i have a bunch of repeating textboxes and comboboxes on an html page. I want to change the value of the textbox below the combobox when i change the combobox. So i have this code so far:
$('.myDropdown').change(function () {
var currentDropdownValue = $(this).val();
if (currentDropdownValue == "Regular") {
//CHANGE TEXTBOX BELOW THIS COMBOBOX WITH CLASS = "QUANTITY" TO 10.
}
});
here is my html
<select class="myDropdown">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<input class="quantity" type="text" />
basically, i need to figure out the right selector syntax as i was using "Closest()" but that seems to only go up the DOM tree and not past the current value.
You could use .next() to return the next item or pass it a selector if you are more picky.
No need for extra DOM Traversing like parent() or such.
Working JSFiddle: http://jsfiddle.net/CXzVe/2/.
If you happen to be updating your DOM after the initial load of the page, you'll need to use
$.live()
or$.delegate()
because jQuery is not aware of the change.JavaScript
HTML
You can try
next()
:You may need to jump around your HTML structure a bit if your input field isn't a sibling of your drop-down, such as
$(this).parent().next('.quantity').val('10')
.Edit: here's a jsFiddle for you.