attaching events to elements in IE does not work when using JavaScript?

D

debrief

Hello,

I have been trying to hide objects on the onclick event and it works
fine in Firefox however IE does not respond at all. Is there a way
around this?

I have the following in my javascript:

div.setAttribute('onclick', "this.style.display = 'none';");


any help is appreciated,
 
R

RobG

Hello,

I have been trying to hide objects on the onclick event and it works
fine in Firefox however IE does not respond at all. Is there a way
around this?

I have the following in my javascript:

div.setAttribute('onclick', "this.style.display = 'none';");

While setAttribute is broken, I don't think the W3C intended you to
pass script in the attribute value parameter. Use:

div.onclick = function(){this.style.display = 'none';}
 
E

emil

(e-mail address removed) pisze:
Hello,

I have been trying to hide objects on the onclick event and it works
fine in Firefox however IE does not respond at all. Is there a way
around this?

I have the following in my javascript:

div.setAttribute('onclick', "this.style.display = 'none';");


any help is appreciated,
You should attach event handlers with addEventListener in ff or
attachEvent in IE.

eg.
function attachE(obj,event,handler) {
if(obj.addEventListener) {
obj.addEventListener(event,handler,false);
} else if(obj.attachEvent) {
obj.attachEvent('on'+event,handler,false);
}
}
AttachE(div,'click',function(){this.style.display='none'});

AFAIK setAttribute does not work this way in IE, but I may be wrong.

Alternatively you can just make it this way:
div.onclick = "this.style.display='none'"

Emil
 
R

RobG

(e-mail address removed) pisze:> Hello,





You should attach event handlers with addEventListener in ff or
attachEvent in IE.

Only if multiple handlers are required, otherwise it is much more
robust to simply assign a value to the appropriate property using dot
notation like:

element.onclick = functionRef;
eg.
function attachE(obj,event,handler) {
if(obj.addEventListener) {
obj.addEventListener(event,handler,false);
} else if(obj.attachEvent) {
obj.attachEvent('on'+event,handler,false);
}}

That doesn't deal with the issue of the this keyword having a
different scope in IE and Mozilla-based browsers, see below.

AttachE(div,'click',function(){this.style.display='none'});

In IE, the this keyword will be a reference to the window object, not
the element. It will cause a script error.

AFAIK setAttribute does not work this way in IE, but I may be wrong.

It doesn't work that way in any browser I know of.

Alternatively you can just make it this way:
div.onclick = "this.style.display='none'"

That doesn't work in any browser I know of.

It a good idea to test things before posting - bad advice is worse
than no advice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top