How to set input value when using select option? Java script with if


User 3199576

I have the following script:

<!DOCTYPE html>
<HTML>
    <HEAD>
        <META charset="UTF-8">
        <SCRIPT src="scripts2.js"></SCRIPT>
    </HEAD>
    <BODY>
        <FORM>
            <SELECT name="receipt" onchange="set_input_value()">
                <OPTION id="P" value="P">P / </OPTION>
                <OPTION id="b" value="b">b / </OPTION>
            </SELECT>
            <INPUT type='text' id='id1' />
        </FORM>
    </BODY>
</HTML>

and

function set_input_value(){
    if(document.getElementById('P').value=='P'){
        document.getElementById('id1').value='1';
    }
    if(document.getElementById('b').value=='b'){
        document.getElementById('id1').value='2';
    }
}

I want to do something similar. If option "P" is selected, set the entered value to 1. If option 'b' is selected, set the entered value to 2.

Sorry if it's been resolved somewhere, but I didn't find it. Thanks for the help.

shadow fox

You need to check the value of the SELECT element. You will get the OPTION element by id and check its value. Change to this:

<!DOCTYPE html>
<HTML>
    <HEAD>
        <META charset="UTF-8">
        <SCRIPT src="scripts2.js"></SCRIPT>
    <SCRIPT type='text/javascript'>
    function set_input_value(){
        if(document.getElementById('receipt').value=='P'){
            document.getElementById('id1').value='1';
        }
        if(document.getElementById('receipt').value=='b'){
            document.getElementById('id1').value='2';
        }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
        <FORM>
            <SELECT id='receipt' name="receipt" onchange="set_input_value()">
                <OPTION id="P" value="P">P / </OPTION>
                <OPTION id="b" value="b">b / </OPTION>
            </SELECT>
        <INPUT type='text' id='id1' />
        </FORM>
    </BODY>
</HTML>

Related


Set value of Select option in Java

Serenity Star Is there a function to set the <select>tag option value ? I have an array of ajax responses and I want to put this array into a <select>label. Here is my code: $.ajax({ type: "GET", url: "http://.......api/1/AbsenceType", dataType: "j

How to set the value of a select option

Ferdinand Barbin I'm trying to populate a dropdown menu based on the selection from the first menu. Use the code below, it will make the next dropdown empty $(\".category_id\").change(function(){ $(\"#account_id > option\").remove(); $(\"#item_name_

How to set the value of a select option

Ferdinand Barbin I'm trying to populate a dropdown menu based on the selection from the first menu. Use the code below, it will make the next dropdown empty $(\".category_id\").change(function(){ $(\"#account_id > option\").remove(); $(\"#item_name_

How to set the value of a select option

Ferdinand Barbin I'm trying to populate a dropdown menu based on the selection from the first menu. Use the code below, it will make the next dropdown empty $(\".category_id\").change(function(){ $(\"#account_id > option\").remove(); $(\"#item_name_

How to set the value of a select option

Ferdinand Barbin I'm trying to populate a dropdown menu based on the selection from the first menu. Use the code below, it will make the next dropdown empty $(\".category_id\").change(function(){ $(\"#account_id > option\").remove(); $(\"#item_name_

How to set the value of a select option

Ferdinand Barbin I'm trying to populate a dropdown menu based on the selection from the first menu. Use the code below, it will make the next dropdown empty $(\".category_id\").change(function(){ $(\"#account_id > option\").remove(); $(\"#item_name_

Set input value on select option in PHP

Avinash Goen I have a form where I want to select products. Based on the selected product, I want to update the rest of the fields in the form. Form display with select options When selecting a product name, I want the rest of the details of the form to be fil

Set input value on select option in PHP

Avinash Goen I have a form where I want to select products. Based on the selected product, I want to update the rest of the fields in the form. Form display with select options When selecting a product name, I want the rest of the details of the form to be fil

Set input value on select option in PHP

Avinash Goen I have a form where I want to select products. Based on the selected product, I want to update the rest of the fields in the form. Form display with select options When selecting a product name, I want the rest of the details of the form to be fil

Set input value on select option in PHP

Avinash Goen I have a form where I want to select products. Based on the selected product, I want to update the rest of the fields in the form. Form display with select options When selecting a product name, I want the rest of the details of the form to be fil

Set input value on select option in PHP

Avinash Goen I have a form where I want to select products. Based on the selected product, I want to update the rest of the fields in the form. Form display with select options When selecting a product name, I want the rest of the details of the form to be fil

Set input value on select option in PHP

Avinash Goen I have a form where I want to select products. Based on the selected product, I want to update the rest of the fields in the form. Form display with select options When selecting a product name, I want the rest of the details of the form to be fil

Set input value on select option in PHP

Avinash Goen I have a form where I want to select products. Based on the selected product, I want to update the rest of the fields in the form. Form display with select options When selecting a product name, I want the rest of the details of the form to be fil

Set input value on select option in PHP

Avinash Goen I have a form where I want to select products. Based on the selected product, I want to update the rest of the fields in the form. Form display with select options When selecting a product name, I want the rest of the details of the form to be fil

Set input value on select option in PHP

Avinash Goen I have a form where I want to select products. Based on the selected product, I want to update the rest of the fields in the form. Form display with select options When selecting a product name, I want the rest of the details of the form to be fil

Set input value on select option in PHP

Avinash Goen I have a form where I want to select products. Based on the selected product, I want to update the rest of the fields in the form. Form display with select options When selecting a product name, I want the rest of the details of the form to be fil

Set input value on select option in PHP

Avinash Goen I have a form where I want to select products. Based on the selected product, I want to update the rest of the fields in the form. Form display with select options When selecting a product name, I want the rest of the details of the form to be fil

Set input value on select option in PHP

Avinash Goen I have a form where I want to select products. Based on the selected product, I want to update the rest of the fields in the form. Form display with select options When selecting a product name, I want the rest of the details of the form to be fil

How to set default value of select option when mapping array

Amanda Conda I am using CustomInput from reactstrap. I am setting the type to be selected. In my CustomInput type="select" I map the options in an array. I need to set the default value in the select to the specific option being mapped. E.g. I like to default