1

The following script is not checking working when testing the radio button

<script type="text/javascript">
      function checkButton(){
          if(document.getElementById('Revise').checked = true){
              alert("hello");
          }
      }
      </script>

The html code is:

<form:radiobutton id= "Revise" value="Revise" name="status" path="status"></form:radiobutton>

Do i need to call the function/or place it in the body?

4
  • You have a typo: .checked = true should be .checked == true or just .checked. You are using the assignment operator instead of the equality operator. Also, I don't see anywhere where you call the checkButton function. Commented Feb 21, 2017 at 22:17
  • You have a typo in document.getElementById('Revise').checked = true row. Use a comparison sign (== or ===) instead of just assigning (=). Commented Feb 21, 2017 at 22:18
  • Are you using Spring MVC? It appears as so with the formatting of your form radio button. Commented Feb 21, 2017 at 22:33
  • @ChrisCruz yes I am Commented Feb 21, 2017 at 22:45

2 Answers 2

1

As most people have mentioned within their comments, you either need to write if(document.getElementById('Revise').checked === true)(newbie way)

or

write if(document.getElementById('Revise').checked) (pro way)

Also, you haven't invoked the function "checkButton", this is how you do it:

<form> <input type="radio" id= "Revise" value="Revise" name="status" path="status" onclick="checkButton()"> Click Me!! </form>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Works fine now :)
you're more than Welcome!!
0

First off, your code is not working because you defined a function (checkButton) that never make a call to, thus it is never executed.

I'm not sure of what you are trying to do but you should avoid using in-line javascript.

If you are trying to run the alert when the radio button is clicked then add an click event listener on the radio.

document.getElementById('Revise').addEventListener('click',function() {
     alert('Hello');
});

JSFindle

If you are trying to define a function called checkButton that checks your radio and shows an alert then your function would be defined like this:

function checkButton() {
    document.getElementById('Revise').checked = true;
    alert('Hello');
}

JSFindle

And then you would just invoke checkButton() on your trigger.

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.