Jump to content

Problem With Adding Event Handler Using Attachevent


E_Spencer
 Share

Recommended Posts

I have the below code. I am trying to add the function chgDI() as the handler for onChange event. However, the reference chgDI(newelement.value), if in parentheses, is not recognized as a function call. If parentheses are left out, it is called only when the drawBOX function is called, and not when the text area changes. Using addEvent results in a 'mismatch' error. Using setAttribute doesn't work either. What can I do?CODE within drawBOX() function:

newelement.attachEvent("change", chgDI(newelement.value));     -OR-newelement.setAttribute("onChange" , "chgDI(newelement.value)");

I've tried both statements with and without parentheses. I've tried change and onchange in the attachEvent parameters. No joy.I'm using Frontpage 2003, and veiwing in IE7

Link to comment
Share on other sites

I have the below code. I am trying to add the function chgDI() as the handler for onChange event. However, the reference chgDI(newelement.value), if in parentheses, is not recognized as a function call. If parentheses are left out, it is called only when the drawBOX function is called, and not when the text area changes. Using addEvent results in a 'mismatch' error. Using setAttribute doesn't work either. What can I do?CODE within drawBOX() function:
newelement.attachEvent("change", chgDI(newelement.value));     -OR-newelement.setAttribute("onChange" , "chgDI(newelement.value)");

I've tried both statements with and without parentheses. I've tried change and onchange in the attachEvent parameters. No joy.I'm using Frontpage 2003, and veiwing in IE7

attachEvent, unlike addEventListener requires you to put the "on" in the event type.attachEvent("onchange", function() { chgDI(newelement.value); })Just so that you know, attachEvent is an Internet Explorer exclusive function. For standard browsers use addEventListener.
Link to comment
Share on other sites

Thanks, that worked like a charm. I was going to ask, but the next post states that this is an IE exclusive statement.Does addEventListener work on IE? If so, why not use it instead? Or do I need to have the page determine which browser is being used, and call the appropriate function?Thanks

newelement.attachEvent("change", function() { chgDI(newelement.value); });

Link to comment
Share on other sites

attachEvent won't work in standard browsers, and addEventListener won't work in Internet Explorer, so you'll have to test to see if it works first, like this:

if(document.addEventListener) {  myElement.addEventListener(... ... ...);} else if(document.attachEvent) {  myElement.attachEvent(... ... ...);} else {  myElement.[onclick, onload, onchange... ]}

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
 Share

×
×
  • Create New...