Solved

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

Posted on 2013-01-28
4
316 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 82

Accepted Solution

by:
leakim971 earned 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
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)

831 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