2

http://jsfiddle.net/jngpjbjm/

Have a look at the fiddle link attached. Radio button value is returning a undefined value. I don't why. Please help with this.

<input type="radio" name="arv" value="1">1
<br>
<input type="radio" name="arv" value="2">2

var radio = document.getElementsByName('arv');
radio[0].addEventListener('click', check());
radio[1].addEventListener('click', check());

function check() {
for (var i = 0; i < radio.length; i++) {
    var rcheck = radio[i].checked;
    if (!rcheck) {
        alert(rcheck.value);

    }

}
}
2
  • Why don't you use jquery? Commented Mar 15, 2015 at 5:27
  • 1
    Why use a whole framework for a simple onclick? In any case the () needs also to be removed from the ,click() Commented Mar 15, 2015 at 5:31

4 Answers 4

2

Try this: http://jsfiddle.net/jngpjbjm/3/

It should be:

alert(radio[i].value);

Maybe you need something like this?

function check() { alert( event.target.value ); } http://jsfiddle.net/jngpjbjm/9/

Sign up to request clarification or add additional context in comments.

3 Comments

You need tp address the wrongly assigned event handler too. Your fiddle - is event available in all browsers?
You were very close though
Second fiddle doesnt work: ReferenceError: event is not defined
2

I have tried to remove all excessive code from your original script as being unnecessary (kind of), whats left are the bare essentials. thanks @mplungjan

Try this:

var radio = document.getElementsByName('arv');
// here is how to add event listeners like the pros over at MDN
radio[0].addEventListener('click', check, false);
radio[1].addEventListener('click', check, false);

  function check(e) {
//simply grab the event by passing it as "e" and capturing its target.value
        var rcheck = e.target.value;
            alert(rcheck);

    }

1 Comment

True. Perhaps mention that? And also that older IEs will not understand the addEventListener
0

Use this

var radio = document.getElementsByName('arv');
radio[0].addEventListener('click', check);
radio[1].addEventListener('click', check);

function check() {
for (var i = 0; i < radio.length; i++) {
    var rcheck = radio[i].checked;
    if (!rcheck) {
        alert(radio[i].value);

    }

}
}

Comments

0

This version will work in all browsers.

window.onload=function() {
  var radios = document.getElementsByName('arv');
  for (var i=0;i<radios.length;i++) {
    radios[i].onclick=function() {
      alert(this.value);
    }
  }
}

onclick

Because it was essentially part of DOM 0, this method is very widely supported and requires no special cross–browser code; hence it is normally used to register event listeners dynamically unless the extra features of addEventListener() are needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.