How do I call same function on clicking either one or more ids?

I have 4 different ids and one function. I am facing an issue when I call that function on clicking either one or more different id attribute.

function checkedCountryStateOpt(){
     $("#country_view").prop('checked', true);
     $("#state_view").prop('checked', true);
}

$("#city_view", "#city_create", "#city_update", "#city_delete").click(function(){
    if($(this).is(":checked")) {
       checkedCountryStateOpt();
        }
});

I could achieve the expected result by calling function on individually on each id. How do I achieve the same result in 5 lines saving 15-20 lines of code.

$("#city_view").change(function(){
    if($(this).is(":checked")) {
    checkedCountryStateOpt();
    }
});

$("#city_create").change(function(){
   if($(this).is(":checked")) {
       checkedCountryStateOpt();
    }
});

$("#city_update").change(function(){
    if($(this).is(":checked")) {
        checkedCountryStateOpt();
    }
});

$("#city_delete").change(function(){
    if($(this).is(":checked")) {
        checkedCountryStateOpt();
        }
});

function checkedCountryStateOpt(){
    $("#country_view").prop('checked', true);
    $("#state_view").prop('checked', true);
}

HTML code is below(just in case)

<!-- location: country master -->
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-sm-2">
                                        <label>Location: Country Master</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <input type="checkbox" name="permissions[]" class="css-checkbox view_check" id="country_view" value="country_view" <?php if(isset($_GET['edit']) && in_array("country_view", $existingPermissionsArray)) { ?> checked <?php } ?> />
                                        <label for="country_view" class="css-label">View</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <input type="checkbox" name="permissions[]" class="css-checkbox action_check" id="country_create" value="country_create" <?php if(isset($_GET['edit']) && in_array("country_create", $existingPermissionsArray)) { ?> checked <?php } ?> />
                                        <label for="country_create" class="css-label">Create</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <input type="checkbox" name="permissions[]" class="css-checkbox update action_check" id="country_update" value="country_update" <?php if(isset($_GET['edit']) && in_array("country_update", $existingPermissionsArray)) { ?> checked <?php } ?> />
                                        <label for="country_update" class="css-label">Update</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <input type="checkbox" name="permissions[]" class="css-checkbox delete action_check" id="country_delete" value="country_delete" <?php if(isset($_GET['edit']) && in_array("country_delete", $existingPermissionsArray)) { ?> checked <?php } ?> />
                                        <label for="country_delete" class="css-label">Delete</label>
                                    </div>
                                    <div class="col-sm-3">
                                        <input type="checkbox" name="permissions[]" class="css-checkbox delete action_check" id="csv_upload_loc" value="csv_upload_loc" <?php if(isset($_GET['edit']) && in_array("csv_upload_loc", $existingPermissionsArray)) { ?> checked <?php } ?> />
                                        <label for="csv_upload_loc" class="css-label">Upload csv (Country-State-City)</label>
                                    </div>
                                </div>
                            </div>

                        <!-- location: state master -->
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-sm-2">
                                        <label>Location: State Master</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <input type="checkbox" name="permissions[]" class="css-checkbox view_check" id="state_view" value="state_view" <?php if(isset($_GET['edit']) && in_array("state_view", $existingPermissionsArray)) { ?> checked <?php } ?> />
                                        <label for="state_view" class="css-label">View</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <input type="checkbox" name="permissions[]" class="css-checkbox action_check" id="state_create" value="state_create" <?php if(isset($_GET['edit']) && in_array("state_create", $existingPermissionsArray)) { ?> checked <?php } ?> />
                                        <label for="state_create" class="css-label">Create</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <input type="checkbox" name="permissions[]" class="css-checkbox update action_check" id="state_update" value="state_update" <?php if(isset($_GET['edit']) && in_array("state_update", $existingPermissionsArray)) { ?> checked <?php } ?> />
                                        <label for="state_update" class="css-label">Update</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <input type="checkbox" name="permissions[]" class="css-checkbox delete action_check" id="state_delete" value="state_delete" <?php if(isset($_GET['edit']) && in_array("state_delete", $existingPermissionsArray)) { ?> checked <?php } ?> />
                                        <label for="state_delete" class="css-label">Delete</label>
                                    </div>
                                </div>
                            </div>

                        <!-- location: city master -->
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-sm-2">
                                        <label>Location: City Master</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <input type="checkbox" name="permissions[]" class="css-checkbox view_check" id="city_view" value="city_view" <?php if(isset($_GET['edit']) && in_array("city_view", $existingPermissionsArray)) { ?> checked <?php } ?> />
                                        <label for="city_view" class="css-label">View</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <input type="checkbox" name="permissions[]" class="css-checkbox action_check" id="city_create" value="city_create" <?php if(isset($_GET['edit']) && in_array("city_create", $existingPermissionsArray)) { ?> checked <?php } ?> />
                                        <label for="city_create" class="css-label">Create</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <input type="checkbox" name="permissions[]" class="css-checkbox update action_check" id="city_update" value="city_update" <?php if(isset($_GET['edit']) && in_array("city_update", $existingPermissionsArray)) { ?> checked <?php } ?> />
                                        <label for="city_update" class="css-label">Update</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <input type="checkbox" name="permissions[]" class="css-checkbox delete action_check" id="city_delete" value="city_delete" <?php if(isset($_GET['edit']) && in_array("city_delete", $existingPermissionsArray)) { ?> checked <?php } ?> />
                                        <label for="city_delete" class="css-label">Delete</label>
                                    </div>
                                </div>
                            </div>

['city_view','city_create','city_update','city_delete'].forEach(function(id){
  document.getElementById(id).addEventListener('change',function(event){
    // on change ...
    console.log(event.detail);
  })
})

The methods I've seen on the Internet would suggest making a new function that calls each function, in turn. Then, call that new function with the onsubmit attribute. Example: <script type="text/javascript"> function DoAllThese() { One(); Two(); Three(); Four(); } </script> Then, call the DoAllThese() function with this attribute value:


You can simply comma-separate the selectors:

$("#city_view, #city_create, #city_update, #city_delete").change(function() {
  if (this.checked) { /* whatever */ }
})

just like you would in CSS.

Another option is using a delegate listener that catches the click event on the document element only, and checks what has been clicked inside the handler:

document.addEventListener('click', function(event) {
  if ( ["city_view", "city_create", "city_update", "city_delete"].includes(e.target.id) ) { /* whatever */ }
})

I am running into a problem applying a function to multiple elements with the same ID at once. Hi There, This happens because you are not allowed to use more than one of a unique ID per page.


You can use selector as single string, It's working for me

$("#city_view,#city_create,#city_update,#city_delete").click(function(){
    if($(this).is(":checked")) {
       checkedCountryStateOpt();
    }
});

A double-click is a special type of left-mouse-button click which is getting used less and less. A double-click is usually used for opening files on your computer, where a single-click is used for navigating web pages. Because so much computer is online, the double-click is declining in use.


May be you can do this .. give a class to your elements and check for the ids later .

$(".All").change(function(){
var id = $(this).attr("id");
if($("#" + id))is(":checked") {
    checkedCountryStateOpt();
    }
});

makes you code less

what's the correct syntax to call 2 functions from an onclick attribute? the code below doesnt work, maybe im putting semi colons in the wrong place? since the second function needs the return


Yet, so far it looks like HTML classes and are ids basically do the same thing, doesn’t it? However, there are differences. It’s no coincidence that above I have used an id for a single element and a class for several. That’s because ids are meant to be unique, classes are not. To be more specific: An HTML element should only have one ID


The function recalculates and produces a new random number every time anyone makes a change to the worksheet, including actions such as adding new data.; Any formula that depends either directly or indirectly on a cell containing a volatile function also recalculates every time someone makes a change in the worksheet.


1. Basic jQuery.each() Function Example. Let’s see how the jQuery.each() function helps us in conjunction with a jQuery object. The first example selects all the a elements in the page and