Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to assign the ID of the checkbox to a variable that is been currently checked or unchecked.And then I want to use that variable I want to check whether the checbox of that ID is checked or unchecked.Can anyone help me coding for this. Till now I have done the followiing code.
JavaScript
$('.checkindustry').click(function () {

               var industryid = $(this).attr('id');
               alert(industryid);
                $('.checkproduct').click(function () {


                    if((industryid.attr('checked',false))&&(this.checked==false)){
                       $("." + $(this).attr("id")).find(".casestudies_contents").hide();
                    }


In the above code am not getting the output. I know I have a mistake in the "if" statement. Please anyone provide an alternate solution for this.
Posted
Updated 28-May-14 21:10pm
v2
Comments
Sampath Lokuge 29-May-14 6:30am    
Have you put a debug and checked ? try with chrome dev tools.
Kaushik.Subramanian 29-May-14 7:16am    
u need the checkbox id to be displayed when its checked?

1 solution

XML
<script type="text/javascript">
    $(document).ready(function () {
        var idval = $(".checkindustry").val();
        $(document).on("click", ".checkindustry", function (e) {
            if ($(this).is(":checked")) {

                alert(idval);

            }
            else {
                alert("unchecked")
            }
        });

    });
</script>


<input type="checkbox" class="checkindustry" value="1" />
 
Share this answer
 
Comments
user 3008 29-May-14 7:16am    
Whenever I use a "if" condition for checking whether it is checked or not for the first time it is working perfectly but for the second time the "if" condition is been converted to normal code statement and hence makes the checkbox unchecked if the checked is checked
Kaushik.Subramanian 29-May-14 7:33am    
did u try with the above code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900