Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using a radio button as a toggle controlled by jQuery/Javascript

Posted on 2013-01-28
4
Medium Priority
?
325 Views
Last Modified: 2013-01-28
I have a simple radio button such as:

<input type="radio" id="noexercisebutton">

Open in new window


What I want to do is:

1. When you click the radio button, it selects.
2. When you click it a second time, it de-selects.
3. Additionally, I have Javascript that performs various tasks based on either (1) or (2), so I need to know which occurred.

I know how to make a radio button select or de-select via Javascript... the problem is that all of my event handlers seem to be conflicting with the inherent behavior of radio buttons. I tried "mouseup", but its unclear which happens first... a browsers handling of a radio button, or the timing of when Javascript sees a radio button as either checked or unchecked.

Is there some way to resolve this, or am I attempting something that is going to cause problems in various browsers?

Thank you!
0
Comment
Question by:bbdesign
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 38826733
why a radio button button and not a checkbox?
0
 

Author Comment

by:bbdesign
ID: 38826816
Hmm, good point. Maybe a checkbox would be a better idea.
0
 

Author Comment

by:bbdesign
ID: 38826827
That's what I'm going to do. Don't know why I didn't think of that, Monday morning and not enough coffee perhaps. Thanks!
0
 
LVL 22

Expert Comment

by:Mrunal
ID: 38826834
Hi
You can use checkbox for this functionality.

Here is example:

http://jsfiddle.net/Zdrnr/
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

916 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