I have a function that writes a value from a select to an input. I am using a live(“change”, function to trigger this.
if ($(this).val( ) == 'MyValue') {$(".my_input").val(VarValue);}
This works fine but I need to target the closest input so it only writes to that one input and not all of them. What I have tried below does not work. Please help if you can, thanks.
if ($(this).val( ) == 'MyValue') {$(this).closest(".my_input").val(VarValue);}
UPDATE
Here is a link to the Jsfiddle, please note, the functionality is not working here, but you can see my html. http://jsfiddle.net/clintongreen/9x6kv/
Your fiddle, but it works :Click here
You had an issue with your js – Change:
to:
Although this doesn’t really address how to select the closest input. In the case of your fiddle, it’s the only element that’s using that class. If this row structure is going to be copied and reused, use this to select the right input.
Edit:
Better yet, use an array to assign the right value rather than having 5 lines of if statments.
add values to the option tags
and then