SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru
    Join Date
    Dec 2008
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check a checkbox with the space bar

    at present the checkbox i have gets checked either by mouseclick or by tab button. instead of tab button i want the checkbox t obe checked by spacebar during runtime.

    is it possible how?

  2. #2
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,873
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    If possible, would have to be javascript, but seems like a bit of an overkill to me

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    England
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with NightStalker, it can be overkill for most people, but if you agree with me that its a huge oversight by browsers that you can't use the keyboard to select checkboxes then the following code might help.

    I have used standard javascript code, but if your using some sort of javascript framework (jQuery, YUI, etc) then use them instead.

    Cross browser code to get KeyCode (IE 5.5+, FF 2+, Opera 9+)
    Code:
    function KeyCode()
    {
    	if(typeof window.event!='undefined')
    	{
    		return window.event.keyCode;
    	}
    	else 
    	{
    		if(typeof event!='undefined')
    		{
    			if(event.keyCode!=0)
    			{
    				return event.keyCode;
    			}
    			else
    			{
    				return event.which;
    			}
    		}
    		else
    		{
    			return 0;
    		}
    	}
    }
    Either use ASP.NET
    Code:
    CheckBox1.Attributes["onkeydown"] = "var k=KeyCode();if(k==32){document.getElementById('" + CheckBox1.ClientID + "').click();}"
    or Attach your own function using javascript.

    Hope this helps

  4. #4
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,873
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    You can use space to select in Firefox, dnt use any other browser, so wouldnt no if you can or not in those. I just think its an overkill as there is a bit of JS on the page, and uses will likely not use it as they are used to just clicking

  5. #5
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by NightStalker-DNS View Post
    You can use space to select in Firefox ...
    ... and every other browser that I've tried which is why this one is a puzzle to me. Any checkbox with focus is toggled using the spacebar.

  6. #6
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    NY, USA
    Posts
    712
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by NAWA-mark View Post
    ... and every other browser that I've tried which is why this one is a puzzle to me. Any checkbox with focus is toggled using the spacebar.
    ditto. I'm confused. tab should lead you through the tab index of form elements. And spacebar always worked for me to toggle a checkbox as far back as I can remember. (assuming it has focus)

  7. #7
    SitePoint Guru
    Join Date
    Dec 2008
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you where right , spacebar toggels the check and uncheck and tab shifts the focus frm one control to another
    but for the checkbox list instead of tab control shifting the focus i want it to be done using down arrow key is it possible

  8. #8
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    NY, USA
    Posts
    712
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it can certainly be done, but I don't think it's something you can do directly with the checkbox list control. You would have to write some custom javascript (at least that's what I would do), to capture the keypress event and move focus on the arrow key presses.

    a search or question in the javascript forum would probably be the way to go.

    I've never added onkeypress attribute to a checkboxlist in .NET so I have no idea what may happen... but if it's not what you wanted - then keep in mind that your custom javascript will need to effect the HTML that gets spit out by .NET.

    My hunch is that you'll be better off NOT trying to interwine this with your asp.NET code.
    But I been wrong before.

  9. #9
    SitePoint Wizard webcosmo's Avatar
    Join Date
    Oct 2007
    Location
    Boston, MA
    Posts
    1,480
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This is not a common user case scenario. Highly not recommended.
    You can catch the key events on server side as well, if you need. Using it with AJAX would hide the postback.

  10. #10
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Why purposely confuse the end user by changing the browser's expected behavior?

  11. #11
    SitePoint Guru
    Join Date
    Dec 2008
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I HAVE READ ALL MSGS BUT NOT GETTING THE DESIRED SOLUTION

    I HAVE A ASPX PAGE WITH FEW CONTROLS
    LIKE TEXTBOXES, DROP DOWN BOXES . ALONG WITH IT I HAVE A CHECKBOX IN A DIV WHICH HAS A SCROLL BAR.i have set the taborder
    when i load the page my tab index is on first textbox
    when i press tab it goes to next control and so on . when it comes to checkbox opuplated with items frm database(suppose i have 50 checkbox in the div) , when my tab comes to the first checkbox , i have to press tab 50 times to come out of the control and go to next control outside the div

    so to avoid it i wanted the tab to shift between the controls and for checkbox ina div i wanted up/down arrow key .

  12. #12
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,873
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Javascript Would Be Solution, Post There


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •