Jump to content

Could someone explain this (about 'onchange' function)?


Lucy

Recommended Posts

If I have a function written up, and I want to use it 'onchange', why does it have to be placed within a function? I'll show you what I mean:

function changeColour() { box.style.color="blue";}box.onchange = function(){ changeColour();};

This would work ^

function changeColour() { box.style.color="blue";}box.onchange = changeColour();

Whereas this wouldn't ^

 

(I think this applies to onclick, etc, too. )

Link to comment
Share on other sites

Normally when you want to assign a function to an element for an event and to have that function ready to be executed whenever the user does something with that element(i.e., onclick etc), you do it the way davej showed without parentheses.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...