JavaScript Why does is my onchange event triggered when my page loads instead of when I click my radio button?

I have the following code in my javascript. I'd like the alert to happen wnen a user checks the radio button, but instead it happens when the page loads, and NOTHING happens when the radio button gets checked. Why?

var myRadioButton = document.getElementById("myRadioButton");
myRadioButton.onchange=alert("Checked!");

The radio button starts off unchecked. In the HTML it looks like this:

<div class="myRadioButton"><input type="radio" id="myRadioButton" name="radioButtons"  />&nbsp; This is my radio button 
</div>

Why on earth would the javascript think an onchange event has occurred when the page loads? And why doesn't it recognize a change when I actually check the radio button?

In fact, whatever element I use with "onchange" makes the alert pop up when the page loads, without any user interaction at all. Am I misunderstanding how "onchange" works?

Answer:1

What you mean to do is

myradiobutton.onchange = function() { alert('Checked'); };

What you did will execute the alert-function and assign the return value of that function as an event handler to the onchange event (which will do nothing when executed)

Answer:2

try

<script>
    var myRadioButton = document.getElementById("myRadioButton");

    myRadioButton.onchange = function () { alert("Checked!") };
</script>
Answer:3

So this is what I am after, and I've been told it isn't possibly but I am not going to give up just yet! Let's say the user types in "London" into my location search box and clicks "Geocode" I am ...

So this is what I am after, and I've been told it isn't possibly but I am not going to give up just yet! Let's say the user types in "London" into my location search box and clicks "Geocode" I am ...

First and foremost, I apologize in advance for the ignorance on my part - I do not know JavaScript at all and I am somewhat new to Stackoverflow and jsFiddle. I did my homework and was able to get the ...

First and foremost, I apologize in advance for the ignorance on my part - I do not know JavaScript at all and I am somewhat new to Stackoverflow and jsFiddle. I did my homework and was able to get the ...

  1. applying javascript to html

I've just started using flot and I'm already clueless how I'll get the points on the right date. First I thought it might be a problem with the daytime, if so.. how do I handle it? var data = [ [...

I've just started using flot and I'm already clueless how I'll get the points on the right date. First I thought it might be a problem with the daytime, if so.. how do I handle it? var data = [ [...

  1. jquery flot chart examples
  2. jquery flot chart zooming
  3. jquery flot chart
  4. jquery flot chart tooltip
  5. jquery flot chart title
  6. jquery flot chart documentation
  7. jquery flot chart tutorial
  8. jquery flot chart xaxis label
  9. jquery flot chart options
  10. jquery flot chart ajax
  11. jquery flot chart download
  12. jquery flot chart pie
  13. jquery flot bar chart
  14. jquery flot line chart
  15. jquery flot bar chart multiple series
  16. jquery flot line chart examples
  17. jquery flot pie chart tooltip
  18. jquery flot bar chart examples
  19. jquery flot pie chart label overlap
  20. jquery flot bar chart tooltip

I would like to limit the user to entering 10 characters into a text area, and to notify the user when they approach the limit. I'm doing this by setting the maxLength attribute of the text area, and ...

I would like to limit the user to entering 10 characters into a text area, and to notify the user when they approach the limit. I'm doing this by setting the maxLength attribute of the text area, and ...

  1. limit characters textarea
  2. textarea limit characters
  3. limit character textarea html
  4. max characters textarea
  5. max characters textarea html
  6. max characters textarea jquery
  7. max characters textarea javascript
  8. limit textarea characters jquery
  9. limit textarea characters javascript
  10. limit textarea characters angular
  11. limit textarea characters per line
  12. limit textarea characters css
  13. textarea max characters
  14. character limit textarea using jquery
  15. character limit textarea input
  16. limit characters in textarea using javascript
  17. js limit characters textarea
  18. limit characters in textarea angularjs
  19. limit max characters textarea
  20. limit number characters textarea