Home > Software engineering >  How I can set the value of a drop down list equal to the value from an input box and disable it for
How I can set the value of a drop down list equal to the value from an input box and disable it for

Time:08-01

I am trying to set the value of a drop down list from the input box value and also trying to disable it. I am able to disable it but unable to set the value of it from the input box. Can anyone please help here.

Here is my HTML code.

<div >
    <label>New Image VM Resource Group</label>
    <input type="checkbox" id="IsAssociation" name="resourcegroupnull" value="New-Resourcegroup">New Resourcegroup</input>
    <select  id="groupunderwriter" name="rgnames_option1">
        <option value="" disabled selected>Select Image VM Resource Group</option>
        <?php 
        foreach ($rgnames as $item) {
            echo "<option value='strtolower($item)'>$item</option>";
        }
        ?>
    </select>

Here is my JavaScript code.

$(document).on('change', '#IsAssociation', function () {
    if ($(this).prop('checked')) {
        $('#groupunderwriter').attr('disabled', 'disabled');
        let rgname = prompt("Please enter resource group name");
        if (rgname != null) {
            $('#groupunderwriter').val('rgname');
        }
    } else {
        $('#groupunderwriter').removeAttr('disabled');
    }
});

CodePudding user response:

use jquery val() property for set and get value for html selector

$('#groupunderwriter').val(rgname);
$('#groupunderwriter').append('<option selected>' rgname '</option>');
  • Related