0

I want to get the value of the current <input type="radio"> element in its onClick.

I need something like:

<input type="radio" name="myRadio" value="something" onClick="theValueOfThisInput">

I can't use getElementById() because all my radio buttons have the same id.

I've currently done it by wrapping all the inputs in a <div> and getting the elements of the <div> by index, but I won't even bother posting it because I don't think it would be helpful to anyone.

Can someone recommend a good way to do this ?

Thanks,

Simeon
  • 7,582
  • 15
  • 64
  • 101
  • 1
    Multiple radio buttons can't have the same id, they must have unique ids (but the same name). – Quentin Jun 13 '11 at 09:22

2 Answers2

5

Have you tried

this.value;

?

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Could be something related to the particular JSP though. Is this the only way ? – Simeon Jun 13 '11 at 09:16
  • How did you try the above code? Are you calling a function onclick? – Ash Burlaczenko Jun 13 '11 at 09:19
  • @Ash Yes. onClick="doSomething(this.value)" – Simeon Jun 13 '11 at 09:21
  • sigh ... I wasted your time sorry, there is another JS function that runs before mine and wipes all the values from several elements ... this is the reason why this.value doesn't work. Nevertheless this answers my question. – Simeon Jun 13 '11 at 09:29
  • Also watc uses of 'onClick': http://stackoverflow.com/questions/4380719/onclick-or-onclick – Ed . Jun 13 '11 at 09:30
1

this.value should certainly work.

Try:

<input type="radio"
        name="myRadio"
        value="something"
        onclick="alert(this.value)" />

And the 'alert' function can be replaced with whatever function call you wish.

Ed .
  • 6,373
  • 8
  • 62
  • 82