Retrieve checked status of two buttons with same class for use in if statement?

how to check which button is clicked in javascript
check if button is clicked jquery
onclick get id jquery
javascript click button by id
how to get button value in jquery
get button value javascript
get id of clicked element jquery
get clicked element javascript

I have multiple sets of radio buttons in the same page, all of which share the same data apart from each set's name.

For the sake of this question, let's say I have three buttons within each set, and that there are 2 sets (in reality there are many more of each).


<input class="radio-1" name="set-1" type="radio">
<input class="radio-2" name="set-1" type="radio">
<input class="radio-3" name="set-1" type="radio">

<input class="radio-1" name="set-2" type="radio">
<input class="radio-2" name="set-2" type="radio">
<input class="radio-3" name="set-2" type="radio">

It's important for me to use the same class name and to not name them each a separate id, because there are so many that it would make coding the functions I will use them for an unbearably long and complex process.

In my JS, I want to be able to say: "if any (even if only one) of the radio buttons that share the 'radio-2' class name are checked, then proceed with function 1. Else, proceed with function 2."

This is what I have in my JS, but it's not working:

var radio2 = document.getElementsByClassName('radio-2');

if (radio2.checked) {

// Perform function 1

  } else {

// Perform function 2


How would you get this working? I'm open to any sort of workaround.

you could use JQuery for that:

// select all DOM elements of type 'input' with css class 'radio-2' that are checked!
if ($(' :checked').size() > 0) {
    //call function 1
} else {
    //call function 2

Human Computer Interaction, To explain the translation of states to code, we will use again the purchase windows sample It arrives at two possible states through four transitions from the state query in a list, a listener operation is generated on the list in the purchase class. For instance, a button-transition Add to cart containing a condition [selected  Technique 1: Multiple Buttons with the Same Name Invoking Default Controller Actions By setting a couple of standard attributes and adding a parameter to the default HttpPost controller action ( ActionResult ), it's easy to get an HTML form to respond differently to different button pushes.

you can use the class with name selector

if ($('radio-2[name=set-2or anything] :checked').size() > 0) {
     //call function 1
} else {
     //call function 2

Infomatic Practices, An if statement allows a program to choose whether or not to execute a following Several cases can execute the same statements by listing the case labels in that they have two states: selected and not selected (also called deselected). 14. The Swing GUI component contains three state button types: JToggleButton,  I can set a radio button to checked fine, but what I want to do is setup a sort of 'listener' that activates when a certain radio button is checked. it adds a checked attribute and all is well, but how would I go about adding an alert. For example, that pops up when the radio button is checked without the help of the click function?

In the end, this worked for me:

if ($('').is(':checked')) {
  //call function 1
} else {
  //call function 2

Borland C++ Builder 6 Developer's Guide, There should also be two buttons on the form. You created a string from the String class named GetListItem, which equals the item that was selected by the user. For our third event, you used an OnkeyPress for the Edit1 edit box. In any case, the if statement checks the passed parameter of Key to see if this is true​. If you want to avoid this altogether, mark one of the radio buttons as checked ( checked="checked") in the HTML code, which would guarantee that one radio button is always selected. Share a link to this answer. improve this answer. edited Sep 14 '09 at 20:57. answered Sep 14 '09 at 20:49. 116 silver badges. 163 bronze badges.

[AJAX & PHP] How to get specific Button Value if all of them have the , idValue="+/*This is where the selected button's id should appear!*/); from an array of buttons which has the same class and use it for the ajax get function! readyState === 4 && xhr.status === 200){ statement block). code in Codepen, JSFiddle, or somewhere else, so we can both see the same code? Retrieve value of radio button from database to form Retrieve value of radio button from database to form var gn=document.getElementsByName("empg"); //empg is name of radio input type.

How to get the ID of the clicked button using JavaScript / jQuery , How to Toggle an Element Class in JavaScript ? Example 1: This example sets a onClick event to each button, when button is clicked, the ID of the button is passed to the function How to check if the clicked element is a div or not in JavaScript ? How to compare two JavaScript array objects using jQuery/​JavaScript ? If you want to use the check box result in a formula, follow these steps to link it to a cell: To select a check box, press the Ctrl key, and click on the check box. Click in the Formula Bar, and type an equal sign =. Click on the cell that you want to link to, and press Enter.

How to Use Buttons, Check Boxes, and Radio Buttons (The Java , To create a button, you can instantiate one of the many classes that descend from the Can be instantiated or subclassed to create two-state buttons. else if (​source == teethButton) { //make a note of it } if (e.getStateChange() == ItemEvent. Each time the user clicks a radio button (even if it was already selected), the  What is the primary difference in the use of radio button and check box controls? Radio buttons work as a group; check boxes work independently. If you want to load values into a combo box when a form is first displayed, where should you put the code that fills the combo box?