jQuery function to check/uncheck a checkbox input type on the basis of select tag option

My HTML Code:

<select id="select">
<option value="Select" selected >Select</option>
<option value="lahore" id="lahore" >Lahore</option>  
<option value="karachi" id="karachi">Karachi</option>
<label> <input class="chkbox" type="checkbox" name="" id="chkbox-lahore" value="Lahore">Lahore</label>
<label> <input class="chkbox" type="checkbox" name="" id="chkbox-karachi" value="Karachi">Karachi</label>

JQuery Code:

var selected=$(this).val();
$(’[id^=“chkbox-”]’).prop(‘checked’, false);
$("#chkbox-"+selected).prop(‘checked’, true);

Code is working fine, since I have just taken it from the google I want to understand what this bold text is actually doing, thanks! Any links to understand this especially what ^ symbol means and - sign after chkbox

Moreover, I have used chkbox class for my checkbox input type not id and to specify it in jQuery we use .chkbox but here it not works, when I write .chkbox Here $(".chkbox-"+selected).prop(‘checked’, true);

$(".chkbox-"+selected).prop(‘checked’, true);

The above would only work if the class names were names like chkbox-karachi or chkbox-lahore. Currently, your checkboxes both have the class name chkbox.

Okay thanks @camperextraordinaire, and what this concept is actually called? Please any topic/online resources you can mention like as I said I have taken this from google so I want to actually understand it

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.