Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8142439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:49:22+00:00 2026-06-06T12:49:22+00:00

Currently Im have the following script which checks to see if a checkbox value

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:49:23+00:00Added an answer on June 6, 2026 at 12:49 pm

    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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following script currently, which gets the category IDs as arrays: $sql_select_categories
I`m currently working on a script, and I have the following situation. function somnicefunction()
Currently, I have a script which does the following. If I have text file
I currently have the following script: http://jsfiddle.net/oshirowanen/mnXdv/ Which works fine, i.e. if a click
I have the following script which runs fine but I think a) it is
I have the following script, which works successfully till the last file slice is
I have the following script working to add odd and even classes to alternating
I have the following script which runs once a day on cron on heroku.
I have the following jquery script, which deals with a query site-visitors enter to
I have the following problem using subversion: I'm currently working on the trunk of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.