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

Discussion in 'Javascript' started by Moses, Mar 18, 2007.

  1. Moses

    Moses Guest

    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
     
    Moses, Mar 18, 2007
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Mar 18, 2007
    #2
    1. Advertising

  3. Moses

    Moses Guest

    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
     
    Moses, Mar 18, 2007
    #3
  4. Moses

    Guest

    On Mar 18, 11:53 am, "Moses" <> 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: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.
     
    , Mar 19, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. meskaline
    Replies:
    0
    Views:
    953
    meskaline
    Nov 2, 2006
  2. Phi!
    Replies:
    1
    Views:
    188
  3. Replies:
    0
    Views:
    108
  4. Replies:
    7
    Views:
    231
  5. donpro
    Replies:
    0
    Views:
    104
    donpro
    Oct 29, 2007
Loading...

Share This Page