Shazma Batool

Shazma Batool

  • NA
  • 101
  • 5.9k

Select an option makes a checkbox checked

May 18 2022 9:52 AM

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)