I am trying to change the input field value dynamically when the user pick the options from my dropdown menu. I have tried this code but have no luck. I was wondering if someone here can help me out! Thanks a lot.
$(document).ready(function(){ $('select[name="job_number"]').change (function () { $('input[name="project"]').val()="Good Fish"; }); }); <form action='project_manager' method='post'> <input type='text' name='project'>show Good Fish when user picks an option</input> <select name='job_number'> <option value='1'>job1</option> <option value='2'>job2</option> <option value='3'>job3</option> </select> </form>
To get a form value with jQuery, you need to use the val() function. To set a form value with jQuery, you need to use the val() function, but to pass it a new value.
jQuery val() Method The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element. When used to set value: This method sets the value of the value attribute for ALL matched elements.
Change the Input Value Using the setAttribute() Function in JavaScript. We can also use the setAttribute() function instead of the value property to set the input value. We can also use the forms() function instead of the getElementById() or querySelector() function to get the element using the form name and input name ...
jQuery val() method is used to get the value of an element. This function is used to set or return the value. Return value gives the value attribute of the first element. In case of the set value, it sets the value of the attribute for all elements.
try :
$('input[name="project"]').val("Good Fish");
instead of:
$('input[name="project"]').val()="Good Fish";
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