tags:

views:

49

answers:

5

I have got an html like this:

<input type="radio" name="v" value="1"> 1<br>
<input type="radio" name="v" value="2"> 2<br>
<input type="radio" name="v" value="3" checked> 3<br>

I want to know how to monitor all of those radio buttons. I could have many more than 3.

One thing I though is to have an 'onclick' function for all of them.

is that the right way ? or is there a neater way to register a common javascript function when the radio button set has changed.

Thanks,

A: 

You can add an event listener to the parent of these elements. Events bubble up the DOM tree so you only need to attach one handler.

See an example here: http://jsfiddle.net/cLzBV/3/

Felix Kling
That Fiddle doesn't work in Firefox :-(
scunliffe
that's interesting.
Lx1
The question said: what's the best way ?
Lx1
in other words, is adding only one handler better than registering as many onclick functions ?
Lx1
The only thing I would do different in the example is to use the 'change' event instead of 'click'
thomasmalt
@Lx1 what is better depends on need and taste. There is no definitive answer, but in this case, to handle a group of radio buttons, one handler is definitly better than many.
thomasmalt
@scunliffe: Mmh indeed, does not work in Firefox... @LX1: Well, if you have a lot radio buttons, you have to add as many event listeners, which takes up more memory.
Felix Kling
@scunliffe: I updated the link, I missed the third parameter for `addEventListener` (Firefox complains about this). Now it works in Firefox too.
Felix Kling
@thomasmalt - Actually **no** you don't want to use the `change` event because **IE** doesn't fire it correctly: http://webbugtrack.blogspot.com/2007/11/bug-193-onchange-does-not-fire-properly.html besides, the "parent" element wouldn't 'change' @Felix Kling is capturing the click event as it is the only applicable event on the parent.
scunliffe
@scunliffe: I didn't know about IE, but the `change` event will definitely bubble up.
Felix Kling
@Felix - I should have drank my first coffee before commenting! ;-) yes, the change will bubble up...
scunliffe
oh.. IE. yes. I always forget about that strain of browser. I hear it is common among users.
thomasmalt
A: 
<input class="someclass" type="radio" name="v" value="1"> 1<br>
<input class="someclass" type="radio" name="v" value="2"> 2<br>
<input class="someclass" type="radio" name="v" value="3" checked> 3<br>

function yourcallback()
{

}
$('.someclass").click(yourcallabck);

This the way you can do using jquery

Markandey Singh
I hate this. Not everybody uses JQuery.
Lx1
+1  A: 

In addition to Markandey's comment: if you are using jquery, you can use attribute selectors instead of classnames without too much hassle so that you have minimal code.

$("input[name=v]").click(mycallback);

In the mycallback function, 'this' will refer to the element that was clicked.

e.g.

mycallback = function(){
  alert($(this).val());
};

Putting an onclick attribute on each element creates a maintenance headache. It also prevents you from treating HTML and JavaScript as separate layers - a data layer and a behaviour layer - in the same way that CSS is your presentation layer.

Listening for events is a more mature way of developing JavaScript. Initially it takes a bit of getting used to (as it is no longer obvious from the element alone that some functionality will get triggered when you click on it) but you soon find your way around that by organising and commenting your code better.

Edited because I saw I hadn't turned the 'this' into a jquery object, and when you are in the callback 'this' is the native DOM object so .val() wouldn't work.

Dawn
Maybe you should mention that you make use of jQuery here.
Felix Kling
A: 

You can use Jquery add class to every radio such as "RadioClass" add jquery file to your page and use the code below...

$(document).ready(function() {
    $('.RadioClass').click(function () { 
        alert($(this).val());
    });
});
Kuntal Basu
+1  A: 

How do I register a javascript event handler to an element that hasn't been added to the page yet... This is a similar post with good solution how to have an 'onclick' function for all of them.

And next code I see useful for you:

document.onclick = myRadioButtonOnClickHandler;

function myRadioButtonOnClickHandler(e) {
    var realTarget = e ? e.target : window.event.srcElement;
    if (realTarget.nodeName.toLowerCase() === 'input' && realTarget.type === 'radio' ) {
        doSomething(); 
    } 
}

Hope it helpful.

mastak