How To Get Checked Check Boxes Values By jQuery

Hi Friends, Some days ago I was trying to take checked values of checkboxes via jQuery while using jQuery Ajax. I tried to find out on internet about how to get checked checkbox values and I found so many articles but didn’t find any clear code which is working good, So i have  tried it myself by taking reference from jquery site and finally I have done this so I thought to share it with my readers.So below is the explanation how to get checked checkboxes values by jQuery.

First of all suppose we have 3 checkboxes as given below:

<input id=”in-category-1″ name=”post_category[]” type=”checkbox” value=”1″ />CakePHP
<input id=”in-category-2″ name=”post_category[]” type=”checkbox” value=”2″ />jQuery
<input id=”in-category-3″ name=”post_category[]” type=”checkbox” value=”3″ />Open Source

Here you will see I have taken checkbox name as array. Now when you want to take value of checked checkbox then you can do it by jquery code given below :

$(':checkbox:checked').each(function(i){
i = parseInt(i);
if($(this).attr('name') == "post_category[]")
{
exclude_category[i] = $(this).val();
}
i++;

});

By above code you can take value of checked checkboxes in exclude_category javascript variable. So you can alert exclude_category and check that values. Let me know if you have any questions regarding this.

Subscribe to PHP Freelancer

Enter your email address: