1

Pretty new to JS and am trying to set up an automator to take care of some tedious tasks on a work website. I've spent many hours today attempting to select the 2nd radio button (of 2) using JS. Seems that the following would do the trick, but have been unsuccessful.

document.getElementById('your_button').click();

Attached is a web inspection screenshot showing that the NAME and ID of the radio buttons are the same. I would like to trigger the TEXT button (HTML is defaulted to checked).

Would certainly appreciate if someone might point me in the right direction.

https://i.stack.imgur.com/G4OOi.png

  • the answer is here. https://stackoverflow.com/questions/15839169/how-to-get-value-of-selected-radio-button – user12345 Oct 03 '17 at 22:40
  • 1
    Make the IDs to be unique. There is no reason for something so simple to be failing. [JSFiddle](https://jsfiddle.net/hescano/vuqh5a15/) - As a side note, make sure that your code is running on _page load_, or place it at the bottom of your page (between the `` tags). – Hanlet Escaño Oct 03 '17 at 23:03

3 Answers3

0

You probably want to add your event listeners like so:

  var yourButton = document.getElementById('your_button');   
  yourButton.addEventListener("change", function() {
    // What you want to happen when event is fired
  });

What we're doing here is that we added the "change" event listener to the radio button, when it's fired it would fall this callback.

Tony Tai Nguyen
  • 1,502
  • 13
  • 27
0

On your second button try onclick="alert('text')" , no need for the javascript prefix. Also, you are doubling up on the double quotes in your onclick function:

onclick="alert("text")" would be read as onclick="alert("

You can avoid this by using both single and double quotes:

onclick="alert('text')"

On a side note, your element IDs should be unique for each element so you (and javascript) can know exactly which element you are trying to select.

Zach A
  • 51
  • 1
  • 6
0

Consider using jQuery, a framework written in JavaScript which gives you shortcuts to do these kinds of things easily and with tiny amounts of code.

It also allows you to (easily) write really neat code because you'd have your event triggers (and all JS code) separate from the main body HTML. https://www.w3schools.com/jquery/ https://jquery.com/

user2542365
  • 81
  • 1
  • 2
  • Thanks for input everyone. My problem is I have no control over the code on the website (3rd party) and the developer did not make button ID's unique. – BusDriverBrian Oct 04 '17 at 17:20
  • My goal is to figure out how to do a "Do Javascript" action that manages to properly click the "text" button. Essentially, automate a lick of the button. – BusDriverBrian Oct 04 '17 at 17:22