views:

82

answers:

2

I need to get the value that has been assigned to the checkbox that is checked , and I've used for loop to show the checkboxes , now i need to access the values of the checkboxes that are checked only ,

Can anybody help me out with this ?

foreach($inbox as $inbox_info) 
{
    <input type="checkbox" id="checkUnread" name="checkUnread" value="<? echo $inbox_info['inbox_id'];?>" />
}

i am trying to do a mail inbox functionality , and i need to get the id of the elements that has its checkbox checked so that i can flag those elements unread in the database

+6  A: 

Check out the :checked selector

$("input:checked").val()

Here's an example function.

function checkValue()
{
    $('.boxes:checked').each(function(){
        alert($(this).val());
    });
}

That works on this set.

<input type="checkbox" class="boxes" value="1" />
<input type="checkbox" class="boxes" value="2" />
<input type="checkbox" class="boxes" value="3" />
<input type="checkbox" class="boxes" value="4" />
<input type="checkbox" class="boxes" value="5" />
<input type="button" onclick="return checkValue()" value="Check Value" />
Ólafur Waage
i need to get the value of each selected checkbox , $("input:checked").val() doesn't seem to get the value
jarus
A: 

It looks like you're html needs looking at.

Id attributes in HTML need to be unique - you are only allowed to use each id once per page. It looks like all your inputs are using the id of 'checkUnread' this will cause bugs in your Javascript (this should probably be the case for name too, unless these values don't get used when the form is submitted).

I suggest you assign your unique inbox_id to be the id of the input. Then instead of getting the value of the input with the Javascript, get the id.

edeverett