简体   繁体   中英

Select and Focus a Checkbox Input using Javascript

I have a checkbox which is auto-generated from php, which is used to filter out search results depending on state. Additionally, a user can select district too to filter out results. In case a person selects a particular district instead of state, i want to automatically select the right state for that district using JavaScript and be able to focus. How can i use JavaScript code to select any checkbox and focus on it using JavaScript? Any assistance is much appreciated.

JSFiddle

<li class="cp_state">
    <label class="title">State</label>
    <div class="handle close dashicons-before"></div>
        <div class="element">   
        <div class="scrollbox">
        <ol class="checkboxes">
            <li><input type="checkbox" name="cp_state[]" value="Johor"  />&nbsp;<label for="cp_state[]">Johor</label></li>
            <li><input type="checkbox" name="cp_state[]" value="Kedah"  />&nbsp;<label for="cp_state[]">Kedah</label></li>
            <li><input type="checkbox" name="cp_state[]" value="Kelantan"  />&nbsp;<label for="cp_state[]">Kelantan</label></li>
            <li><input type="checkbox" name="cp_state[]" value="Wilayah Persekutuan Kuala Lumpur"  />&nbsp;<label for="cp_state[]">Wilayah Persekutuan Kuala Lumpur</label></li>
            <li><input type="checkbox" name="cp_state[]" value="Wilayah Persekutuan Labuan"  />&nbsp;<label for="cp_state[]">Wilayah Persekutuan Labuan</label></li>
            <li><input type="checkbox" name="cp_state[]" value="Melaka"  />&nbsp;<label for="cp_state[]">Melaka</label></li>
            <li><input type="checkbox" name="cp_state[]" value="Negeri Sembilan"  />&nbsp;<label for="cp_state[]">Negeri Sembilan</label></li>
            <li><input type="checkbox" name="cp_state[]" value="Pahang"  />&nbsp;<label for="cp_state[]">Pahang</label></li>
            <li><input type="checkbox" name="cp_state[]" value="Wilayah Persekutuan Putra Jaya"  />&nbsp;<label for="cp_state[]">Wilayah Persekutuan Putra Jaya</label></li>
            <li><input type="checkbox" name="cp_state[]" value="Perlis"  />&nbsp;<label for="cp_state[]">Perlis</label></li>
            <li><input type="checkbox" name="cp_state[]" value="Pulau Pinang"  />&nbsp;<label for="cp_state[]">Pulau Pinang</label></li>
            <li><input type="checkbox" name="cp_state[]" value="Perak"  />&nbsp;<label for="cp_state[]">Perak</label></li>
            <li><input type="checkbox" name="cp_state[]" value="Sabah"  />&nbsp;<label for="cp_state[]">Sabah</label></li>
            <li><input type="checkbox" name="cp_state[]" value="Selangor"  />&nbsp;<label for="cp_state[]">Selangor</label></li>
            <li><input type="checkbox" name="cp_state[]" value="Sarawak"  />&nbsp;<label for="cp_state[]">Sarawak</label></li>
            <li><input type="checkbox" name="cp_state[]" value="Terengganu"  />&nbsp;<label for="cp_state[]">Terengganu</label></li>
        </ol> <!-- #checkbox-wrap -->
        </div> <!-- #end scrollbox -->
        </div> <!-- #end element -->
    <div class="clr"></div>
</li>

When user chooses district you need to know which state are you looking for. Having that you can write simple function that iterates over a set of checkboxes and selects specific one based on the provided value. Every time a user selects the district you need to call function that toggles the checkboxes. Like this:

var checkboxes = document.querySelectorAll('input[type="checkbox"]')

function selectCheckboxWithValue(value) {
    checkboxes.forEach(function(checkbox) {
      if (checkbox.value === value) {
          checkbox.checked = true
          checkbox.focus()
      } else {
          checkbox.checked = false
      }
  })
}

// let's find checkbox with value "Kedah", select it and focus
selectCheckboxWithValue('Kedah')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM