How to select input value from onchange using jQuery?


Red hair

I have a select and input label. I am updating the value of a text input based on what is selected in the select tag.

select label

<select class="form-control form-field-username form-field-users">
<option class="form-field-first-option" value="0" selected="selected">Select User...</option>
<option value="1">Peter Fel</option> 
<option value="2">Mark Getty</option>
</select>

input tag

<input name="user_id" id="user_id" class="form-control form-field-user-id" type="number">

The code selected by onchange:

$('.form-field-username').change( function() {
    $(this).find(":selected").each(function () {
        var userId = $(this).val();

        $('.form-field-user-id').val(userId);

    });

});

I want to set the value of the text input with user id "user_id" to null or undefined if onchange selects the first value or user_id == 0.

Do you know how to modify the code? Any help is appreciated. thanks

Pranav C Balan

Use the ternary operator to check the value in the handler . Since there is only one option to choose from, the method here is completely unnecessary.each()

$('.form-field-username').change(function() {
  $('.form-field-user-id').val(this.value == 0 ? '' : this.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="form-control form-field-username form-field-users">
<option class="form-field-first-option" value="0" selected="selected">Select User...</option>
<option value="1">Peter Fel</option> 
<option value="2">Mark Getty</option>
</select> 
<input name="user_id" id="user_id" class="form-control form-field-user-id" type="number">

Related


How to select input value from onchange using jQuery?

Red hair I have a select and input label. I am updating the value of a text input based on what is selected in the select tag. select label <select class="form-control form-field-username form-field-users"> <option class="form-field-first-option" value="0" sel

How to select input value from onchange using jQuery?

Red hair I have a select and input label. I am updating the value of a text input based on what is selected in the select tag. select label <select class="form-control form-field-username form-field-users"> <option class="form-field-first-option" value="0" sel

How to select input value from onchange using jQuery?

Red hair I have a select and input label. I am updating the value of a text input based on what is selected in the select tag. select label <select class="form-control form-field-username form-field-users"> <option class="form-field-first-option" value="0" sel

How to select input value from onchange using jQuery?

Red hair I have a select and input label. I am updating the value of a text input based on what is selected in the select tag. select label <select class="form-control form-field-username form-field-users"> <option class="form-field-first-option" value="0" sel

Select input by value from list using JQuery

Chris Nevill I have a list that looks like this: <ul id='mylist'> <li> <input val="1"> </li> <li> <input val="2"> </li> </ul> How to select input by value using JQuery? I can get lee's list by $("#mylist").children('li') But c

Select input by value from list using JQuery

Chris Nevill I have a list that looks like this: <ul id='mylist'> <li> <input val="1"> </li> <li> <input val="2"> </li> </ul> How to select input by value using JQuery? I can get lee's list by $("#mylist").children('li') But c

How to select empty input (value="") using jQuery

Richard Harris: How can I check ( ) input fields in a requiredsection for null values and then add a class to them in the event using jQuery? So far I have tried: jQuery("#sender_container input.required").val("").addClass("error"); But that seems to be setti

How to select empty input (value="") using jQuery

Richard Harris: How can I check ( ) input fields in a requiredsection for null values and then add a class to them in the event using jQuery? So far I have tried: jQuery("#sender_container input.required").val("").addClass("error"); But that seems to be setti

How to select empty input (value="") using jQuery

Richard Harris: How can I check ( ) input fields in a requiredsection for null values and then add a class to them in the event using jQuery? So far I have tried: jQuery("#sender_container input.required").val("").addClass("error"); But that seems to be setti

How to get value from Select input using jQuery after DOM is loaded

Js · 古巴斯 (Andrejs Gubars) I am trying to get the value of a selectbut it keeps returning undefined. I've been in a lot of places but can't seem to find a solution. I know it has something to do with the DOM loading before and not seeing mine select. The proble

How to get value from Select input using jQuery after DOM is loaded

Js · 古巴斯 (Andrejs Gubars) I am trying to get the value of a selectbut it keeps returning undefined. I've been in a lot of places but can't seem to find a solution. I know it has something to do with the DOM loading before and not seeing mine select. The proble

How to get value from Select input using jQuery after DOM is loaded

Js · 古巴斯 (Andrejs Gubars) I am trying to get the value of a selectbut it keeps returning undefined. I've been in a lot of places but can't seem to find a solution. I know it has something to do with the DOM loading before and not seeing mine select. The proble

Display input box using jQuery's Onchange select box

love to code I have a simple code for showing input box onChange using jquery. <select id="selectBox" onchange="changeFunc();"> <option value="MCA">MCA</option> <option value="MBA">MBA</option> <option value="not_listed">Not Listed</option> </selec

Display input box using jQuery's Onchange select box

love to code I have a simple code for showing input box onChange using jquery. <select id="selectBox" onchange="changeFunc();"> <option value="MCA">MCA</option> <option value="MBA">MBA</option> <option value="not_listed">Not Listed</option> </selec

Display input box using jQuery's Onchange select box

love to code I have a simple code for showing input box onChange using jquery. <select id="selectBox" onchange="changeFunc();"> <option value="MCA">MCA</option> <option value="MBA">MBA</option> <option value="not_listed">Not Listed</option> </selec

Display input box using jQuery's Onchange select box

love to code I have a simple code for showing input box onChange using jquery. <select id="selectBox" onchange="changeFunc();"> <option value="MCA">MCA</option> <option value="MBA">MBA</option> <option value="not_listed">Not Listed</option> </selec

Display input box using jQuery's Onchange select box

love to code I have a simple code for showing input box onChange using jquery. <select id="selectBox" onchange="changeFunc();"> <option value="MCA">MCA</option> <option value="MBA">MBA</option> <option value="not_listed">Not Listed</option> </selec

How to select/use value from datetime input in JQuery?

dad I am new to coding. How to select value from datetime input using JQuery? I need to use it for further calculations. thanks <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="d04" type="datetime-local" name=

How to select input using jquery?

Effigyon I want to get the value of the input, I want to do it with jquery. I only want to have name="picture"on click . I've used functions to go beyond that. But when i'm in a div . I can't get into the div and select the value of the hidden input inside it