8
Answers

Select an option makes a checkbox checked

Shazma Batool

Shazma Batool

2y
731
1

I want to select an option from the select tag in html containing 2 options and based on the option I have selected a related checkbox gets checked how can I achieve this? Here is my code:

Html:

      

 <select id="select" onclick="Select()">

        <option value="Select" selected >Select</option>

        <option value="Lahore" id="lahore" >Lahore</option>

        <option value="Karachi" id="karachi">Karachi</option>

    </select>

Javascript:

function Select()

{

var Option=document.getElementById("select").value;

if(Option="Lahore"){

    document.getElementById("chkbox-lahore").checked = true;

}

else{

    document.getElementById("chkbox-karachi").checked = true;

}

}

PS. How can I do this with jQuery? Thanks

Answers (8)