Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > DOM onClick event (works in FF and does not work in IE)

Reply
Thread Tools

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

 
 
Moses
Guest
Posts: n/a
 
      03-18-2007
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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-18-2007
Moses wrote:

> 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();
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Moses
Guest
Posts: n/a
 
      03-18-2007
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:voidCr ap";

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_ele...tattribute.asp


Thanks&Regards
Moses



 
Reply With Quote
 
scripts.contact@gmail.com
Guest
Posts: n/a
 
      03-19-2007
On Mar 18, 11:53 am, "Moses" <(E-Mail Removed)> wrote:
> 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:voidCr ap";
>
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JavaScript errors trying to add event listeners for onclick() using DOM and AJAX donpro Javascript 0 10-29-2007 05:29 PM
How do I add a class to an <a> tag on a onclick() event using Javascipt/DOM? SM Javascript 4 05-26-2007 04:07 AM
onclick event in DOM table Michael_R_Banks Javascript 2 02-17-2007 07:22 AM
onclick event does not work with select option in IE prash.marne@gmail.com Javascript 7 01-18-2007 03:35 PM
onclick event does not work with select option in IE prash.marne@gmail.com Javascript 0 01-18-2007 07:16 AM



Advertisments