DOM onClick event (works in FF and does not work in IE)

M

Moses

Hi Everybody,

I have a problem with onClick event which works in FF and does not
work in IE, Here I have giving the details Please help.

I am creating a <a> Tag.

dom_obj = document.createElement('a');
dom_obj.setAttribute('href', 'javascript:void(0)');
dom_obj.setAttribute('onclick', 'javascript:test()');
val = document.createTextNode('Click');
dom_obj.appendChild(val);

The output is

<a href="javascript:void(0)"
onclick="javascript:test()">Click</a>

Now as everybody knows on Clicking the link the function
test() should be called.

The problem is it works in FF and it does not work in IE,
meanwhile if I change href="javascript:test()" the function is called
properly. The thing is IE is not firing the onclick event.

I tried a lot but I could not find out what is the reason
for that , So looking forward for your help.


Thanks & Regards
Moses
 
M

Martin Honnen

Moses said:
I am creating a <a> Tag.

dom_obj = document.createElement('a');
dom_obj.setAttribute('href', 'javascript:void(0)');
dom_obj.setAttribute('onclick', 'javascript:test()');

document.onclick = test;
or
document.onclick = function (evt) {
test();
}
 
M

Moses

Hi All,

Thanks for your reply. Its working fine.

Again I need a clarification Still now I knew that to add an
attribute we should use setAttribute(), but we where also able to add
attribute directly dom_obj.onclick = test. Is there and difference
between this and which is the standard way to add the attribute.



dom_obj.href =
"someWhereUsefulOtherThanThatJavascript:voidCrap";

I didnt understood what do you mean here, But why I use
javascript:void(0) is to avoid the href action after calling the
onclick event.

I bet you didn't search the archives for setAttribute and the problems
with it in IE.

Yes Its true, I only referred http://www.w3schools.com/dom/met_element_setattribute.asp


Thanks&Regards
Moses
 
S

scripts.contact

Hi All,

Thanks for your reply. Its working fine.

Again I need a clarification Still now I knew that to add an
attribute we should use setAttribute(), but we where also able to add
attribute directly dom_obj.onclick = test. Is there and difference
between this and which is the standard way to add the attribute.

To add event listener
The standard W3C method is- addEventListener (doesnt work in IE)
Standard IE method is- attachEvent (works in IE only)
anthoer method is- element.onclick=something (works in all browsers,
afaik)

dom_obj.href =
"someWhereUsefulOtherThanThatJavascript:voidCrap";

I didnt understood what do you mean here, But why I use
javascript:void(0) is to avoid the href action after calling the
onclick event.

What if JS is disabled ? The users will get some strange
message(depends on browser).

So if you assume that js is enabled, return false-
<a href="x" onclick="xxx();return false">
or
<a href="x" onclick="return xxx()"> //the function should return
false

again, what if JS is disabled ? Browser will try to open x that
doesn't exist .. solution -
<a href="someInformation.htm" onclick="xxx();return false">

in someInformation page, tell the users that they should enable JS if
they want to see the link. Simple.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top