Solved

Find the last checkbox checked

Posted on 2008-06-13
7
315 Views
Last Modified: 2008-06-17
I have a group of 3 checkbox. The user can click on any check box,but I want to track the last checkbox checked. The user can check checkbox 1,3 and 2. I want to do this on a button click event. Then I need the id 2. Izt possible in javascript.
0
Comment
Question by:ip32
7 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 21779522
If they all have the same "name" attribute, this is easy.

  var group = document.getElementsByName( "groupName" );
  var last = null;
  for ( var i = 0; i < group.length; i++ ) {
    if ( group[ i ].checked ) {
      last = group[ i ];
    }
  }
  if ( last ) {
    alert( 'last checked: ' + last.value );
  }
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21779721
Very interesting question. What if the user does this:
Checked 1, 3, and 2, then unchecked 2. What Id do you need?
0
 
LVL 33

Expert Comment

by:raterus
ID: 21780030
I did something similar a long time ago.  I ended up appending to a hidden field an indicator that the box was checked.  If they unchecked the box, I searched the hidden field and removed the indicator.  On a postback, I parsed the list and determined the check order.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 41

Accepted Solution

by:
HonorGod earned 250 total points
ID: 21780522
Sorry for my incomplete understanding of the issue.

As suggested, I would have a field that keeps a list of the checked boxes, in stack fashion, and remove the instance from the list when the item was unchecked.

For example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<title> Last checked </title>

<script type='text/javascript'>

  var checked = [];

  function check( id ) {

    var ele = document.getElementById( id );

    if ( ele && ele.checked ) {

      checked.push( id );

    } else {

      for ( var i = 0; i < checked.length; i++ ) {

        if ( checked[ i ] == id ) {

          checked.splice( i, 1 );

          break;

        }

      }

    }

    var debug = document.getElementById( 'debug' );

    if ( debug ) {

      var copy = checked;

      copy.reverse();

      debug.innerHTML = 'checked order: ' + copy.toString();

    }

  }

</script>

</head>

<body>

<table border='1'>

  <tbody>

    <tr>

      <td>Checkbox 0:<input type='checkbox' id='cb0' onclick='check(this.id)'></td>

    </tr>

    <tr>

      <td>Checkbox 1:<input type='checkbox' id='cb1' onclick='check(this.id)'></td>

    </tr>

    <tr>

      <td>Checkbox 2:<input type='checkbox' id='cb2' onclick='check(this.id)'></td>

    </tr>

    <tr>

      <td>Checkbox 3:<input type='checkbox' id='cb3' onclick='check(this.id)'></td>

    </tr>

    <tr>

      <td>Checkbox 4:<input type='checkbox' id='cb4' onclick='check(this.id)'></td>

    </tr>

    <tr>

      <td>Checkbox 5:<input type='checkbox' id='cb5' onclick='check(this.id)'></td>

    </tr>

    <tr>

      <td>Checkbox 6:<input type='checkbox' id='cb6' onclick='check(this.id)'></td>

    </tr>

    <tr>

      <td>Checkbox 7:<input type='checkbox' id='cb7' onclick='check(this.id)'></td>

    </tr>

    <tr>

      <td>Checkbox 8:<input type='checkbox' id='cb8' onclick='check(this.id)'></td>

    </tr>

    <tr>

      <td>Checkbox 9:<input type='checkbox' id='cb9' onclick='check(this.id)'></td>

    </tr>

  </tbody>

</table>

<div id='debug'></div>

</body>

</html>

Open in new window

0
 

Author Comment

by:ip32
ID: 21781113
Excellent. Izt possible to acheive this in asp.net since there is no click event. I do not want to go to the code behind and give the click event..
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21781335
probably, but unfortunately, I know nothing about ASP.NET.
I saw the question because of the "javascript" tag...
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21803571
Hopefully you were able to use the information to help you resolve the issue
using ASP.  Thanks for the grade & points.

Good luck & have a great day
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Log4Net custom Appender not being fired. 5 75
Hovering effect 9 47
Convert to Hour/minute textbox and compare 3 53
Problem to page 4 74
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now