35

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>

2 Answers 2

83

try :

$('input[name="project"]').val("Good Fish");

instead of:

$('input[name="project"]').val()="Good Fish";
Sign up to request clarification or add additional context in comments.

1 Comment

This assumes that there is only 1 form on the page with an input named 'project'. That is true 99% of the time, just good to be aware of it.
17

The code below should work. I modified the <input> in the html to be formatted correctly as well as changed $('input[name="project"]').val()="Good Fish"; to $('input[name="project"]').val("Good Fish");

$(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' value='show Good Fish when user picks an option' />
    <select name='job_number'>
        <option value='1'>job1</option>
        <option value='2'>job2</option>
        <option value='3'>job3</option>
    </select>
</form>

1 Comment

Thanks for the help Keith. I wanted to give you green since you also corrected my html, but I already promised aletzo. +1 thought.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.