Calling onclick on a radiobutton list using javascript
Asked Answered
S

7

66

How do I call onclick on a radiobutton list using javascript?

Shemikashemite answered 12/12, 2008 at 12:30 Comment(0)
A
44

How are you generating the radio button list? If you're just using HTML:

<input type="radio" onclick="alert('hello');"/>

If you're generating these via something like ASP.NET, you can add that as an attribute to each element in the list. You can run this after you populate your list, or inline it if you build up your list one by one:

foreach(ListItem RadioButton in RadioButtons){
    RadioButton.Attributes.Add("onclick", "alert('hello');");
}

More info: http://www.w3schools.com/jsref/event_onclick.asp

Amygdalate answered 12/12, 2008 at 12:40 Comment(1)
Don't you mean foreach(ListItem RadioButton in RadioButtons.Items) ?Uninspired
D
27

To trigger the onClick event on a radio button, invoke the click() method on its DOM element:

document.getElementById("radioButton").click()

using jQuery:

$("#radioButton").click()

AngularJs:

angular.element('#radioButton').trigger('click')
Domiciliate answered 23/11, 2011 at 15:0 Comment(2)
There is however one thing with the .click(): If you change the selected value of a radio with javascript like this, the 'change' event does not fire in IE (I tried IE8)Pycnometer
@Pycnometer thanks a lot, that comment solved a very tricky problem for me! Posted a question/answer about it - https://mcmap.net/q/297605/-how-to-select-asp-net-radiobutton-with-javascript-when-autopostback-is-on ps. behavior is also present in chromeElectrochemistry
F
18

I agree with @annakata that this question needs some more clarification, but here is a very, very basic example of how to set up an onclick event handler for the radio buttons:

window.onload = function() {
  var ex1 = document.getElementById('example1');
  var ex2 = document.getElementById('example2');
  var ex3 = document.getElementById('example3');

  ex1.onclick = handler;
  ex2.onclick = handler;
  ex3.onclick = handler;
}

function handler() {
  alert('clicked');
}
<input type="radio" name="example1" id="example1" value="Example 1" />
<label for="example1">Example 1</label>
<input type="radio" name="example2" id="example2" value="Example 2" />
<label for="example1">Example 2</label>
<input type="radio" name="example3" id="example3" value="Example 3" />
<label for="example1">Example 3</label>
Flossieflossy answered 12/12, 2008 at 12:46 Comment(0)
C
7

The problem here is that the rendering of a RadioButtonList wraps the individual radio buttons (ListItems) in span tags and even when you assign a client-side event handler to the list item directly using Attributes it assigns the event to the span. Assigning the event to the RadioButtonList assigns it to the table it renders in.

The trick here is to add the ListItems on the aspx page and not from the code behind. You can then assign the JavaScript function to the onClick property. This blog post; attaching client-side event handler to radio button list by Juri Strumpflohner explains it all.

This only works if you know the ListItems in advance and does not help where the items in the RadioButtonList need to be dynamically added using the code behind.

Cyanosis answered 18/12, 2008 at 16:36 Comment(1)
To make things worse, the onClick doesn't come up in Intellisense either. Thanks, this helped.Rokach
C
5

I think all of the above might work. In case what you need is simple, I used:

function checkRadio(name) {
  if (name == "one") {
    console.log("Choice: ", name);
    document.getElementById("one-variable-equations").checked = true;
    document.getElementById("multiple-variable-equations").checked = false;

  } else if (name == "multiple") {
    console.log("Choice: ", name);
    document.getElementById("multiple-variable-equations").checked = true;
    document.getElementById("one-variable-equations").checked = false;
  }
}
<div class="radio-buttons-choice" id="container-3-radio-buttons-choice">
  <input type="radio" name="one" id="one-variable-equations" onclick="checkRadio(name)"><label>Only one</label><br>
  <input type="radio" name="multiple" id="multiple-variable-equations" onclick="checkRadio(name)"><label>I have multiple</label>
</div>
Carrier answered 23/3, 2019 at 16:23 Comment(0)
S
4

Try the following solution

$(document).ready(function() {
  $('.radio').click(function() {
    document.getElementById('price').innerHTML = $(this).val();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="variant">
  <label><input type="radio" name="toggle" class="radio" value="19,99€"><span>A</span></label>
  <label><input type="radio" name="toggle" class="radio" value="<<<"><span>B</span></label>
  <label><input type="radio" name="toggle" class="radio" value="xxx"><span>C</span></label>
  <p id="price"></p>
</div>
Stillhunt answered 19/5, 2017 at 21:21 Comment(1)
i like your solution since i don't have duplicate click functions on every radio button. reminder to add jquery like i did in codesandbox with this tag <script src="ajax.googleapis.com/ajax/libs/jquery/3.6.0/…>Screwball
S
0

The other answers did not work for me, so I checked Telerik's official documentation it says you need to find the button and call the click() function:

function KeyPressed(sender, eventArgs) {
  var button = $find("<%= RadButton1.ClientID %>");
  button.click();
}
Satirical answered 9/9, 2019 at 7:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.