samedi 17 décembre 2016

Javascript getElementById when you dont know the ID

I am using PHP to assign a random ID to a checkbox, now I want to check that value of checkbox but because I don't know the ID, I dont know how to check it.

so here is the HTML :

<input class="citycheck" 
       id="<?php $city_id; // this is randomly generated ?>" 
       type="checkbox" 
       name="<?php echo $city_name"> <?php echo $city ?>

<div class"properties">Some Properties</div>

and my Javascript:

<script>
    jQuery(document).ready(function($) {
        // right now I am checking by class
        // show or hide if it is already checked
        if ($(".citycheck").is(':checked'))
            $(".properties").show();
        else
            $(".properties").hide();

        // show or hide if user clicked on the checkbox
        $(".citycheck").click(function() {
            if ($(this).is(":checked")) {
                $(".properties").show();
            } else {
                $(".properties").hide();
            }
        });
    });
</script>

but because there are many checkbox with class citycheck when one is checked, if another city is not checked, the properties are still shown.

How can I check the value of checkbox by ID when I don't know the ID yet?




Aucun commentaire:

Enregistrer un commentaire