Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
165
rated 0 times [  169] [ 4]  / answers: 1 / hits: 18526  / 12 Years ago, wed, june 27, 2012, 12:00:00

Currently Im have the following script which checks to see if a checkbox value has changed but its not working when I try to use it!



<script>
$('input[type=checkbox]').change(function () {
if ($(this).is(':checked')) {
if ($(this).prev().attr('checked') && $(this).val() != $(this).prev().val()) {
alert(previous checkbox has same value);
}
}
});​
</script>

<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)​


you can see it working here yet it does not work when i try to use it
http://jsfiddle.net/rajaadil/LgxPn/7



The idea is to alert when a checked checkbox value is different from the previously checked checkbox value!



Currently my checkbox look like



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


I thought the function ('input[type=checkbox]').change(function() would get these but im wrong somewhere?


More From » jquery

 Answers
10

To select the previous checked sibling checkbox, use this:



$(this).prevAll(:checked:first)


I expected to use :last, but :first is what works. This is counter-intuitive to me and inconsistent with how :first and :last usually work, but I tested it in several browsers and the result is consistent.



$(':checkbox').change(function() {
if (this.checked) {
var lastChecked = $(this).prevAll(:checked:first);
if (this.value == lastChecked.val()) {
alert(previous checked box has same value);
}
}
});


Demo: http://jsfiddle.net/LgxPn/15/






Edit: If by previously checked checkbox you mean the last box the user clicked, then you'll need to keep track of that yourself. There's no built-in jQuery method that will tell you anything about click history.



What happens when the user first checks several boxes, and then checks and immediately unchecks a box? Should the next most recently checked box be used? If so, then you need to keep track of all checked boxes, and what order they were clicked. Here's how you can keep track of the checked boxes:



var lastChecked = [];
$(':checkbox').change(function() {
if (this.checked) {
if (lastChecked.length && this.value == lastChecked[0].value) {
alert(the last box you checked has the same value);
}
lastChecked.unshift(this);
}
else {
lastChecked.splice(lastChecked.indexOf(this), 1);
}
});​


Demo: http://jsfiddle.net/LgxPn/21/


[#84617] Tuesday, June 26, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
richardaydenc

Total Points: 148
Total Questions: 125
Total Answers: 98

Location: Seychelles
Member since Mon, Jun 28, 2021
3 Years ago
richardaydenc questions
Fri, Dec 4, 20, 00:00, 4 Years ago
Thu, Jul 2, 20, 00:00, 4 Years ago
;