Lucy Posted March 21, 2015 Share Posted March 21, 2015 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 More sharing options...
davej Posted March 21, 2015 Share Posted March 21, 2015 Try... box.onchange = changeColour; Link to comment Share on other sites More sharing options...
Don E Posted March 21, 2015 Share Posted March 21, 2015 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now