user1479043
user1479043

Reputation:

How to get previous checkbox value and compare

I am trying to create a checkbox limit based on a value change example: I have the following checkbox!

If the value of a checked checked box is different then the previous prompt an alert! Some of the check boxes do have the same value. Not all of them!

Example:

<input name="" type="checkbox" value="here">(if this was checked)
<input name="" type="checkbox" value="here">(then this)
<input name="" type="checkbox" value="there">(would not allow prompt alert) 
<input name="" type="checkbox" value="here">(would allow)

<input type="checkbox" name="checkbox2[]" onClick="setChecks(this)" value="`key`=<?php 
echo $rspatient['key']?>" class="chk" id="chk<?php echo $a++?>" />

I have code that limits the number of checkboxes but I'm not sure how to compare previous values to the selected.

Upvotes: 0

Views: 4440

Answers (1)

dcpomero
dcpomero

Reputation: 999

You probably want to make use of the prev() and next() jQuery functions. I don't understand well enough what you want to do, but something like $(':checkbox').change(function() { $(this).prev(); //this references the previous sibling }) would get you started

Maybe something like

$('input:checkbox').change(function() {
    if ($(this).attr('checked') && $(this).prev().attr('checked') && $(this).attr('value') != $(this).prev().attr('value')) {
         alert('you can't do that');
    }
});

But like I said, i don't know what you're trying to do

Upvotes: 1

Related Questions