SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I can not add onclick property with setAttribute on IE.

    I am using the following code to create an 'a' element with 'texto' to add the onclick property to an object:

    var vinculo = xCreateElement('a');
    var nodoTexto = document.createTextNode('texto');
    xAppendChild(vinculo, nodoTexto);
    xAppendChild(celda2, vinculo);
    vinculo.setAttribute('onclick', 'buscarSolucion(1,1,1)');

    'celda2' is a table cell. This works with FF but not with IE. I solved this using a comment from here for the 'name' attribute, like this:

    ...
    vinculo = xCreateElement('<a onClick="' + onClick + '">');
    ...

    But I would like to put it in a function to make it general for any element that has the 'onclick' property. Is there any other way to set the 'onclick' attribute & to be understand by IE & FF?

    NOTE: I also found that I can not set 'colspan', I have to set colSpan. Also, I can not set 'class' I have to use the canonical method with 'object.className=...'

  2. #2
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I am not sure why you are doing it that way... document.createElement is more universal...

    i'd use something like this:
    Code:
    var aElement = document.createElement('a');
    aElement.setAttribute('onclick', 'buscarSolucion(1,1,1)');
    aElement.innerHTML = 'SomeLink';
    aElement.setAttribute('href', 'somelocation.htm');
    document.body.appendChild(aElement);
    That is the most uneveral way of createing an element. You do not need to assign it to document.body just replace that with an element you wish the aElement to go into.
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site

  3. #3
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You've run into things that IE doesn't yet support.

    Use DOM0:
    vinculo.onclick = funcRef;

    or DOM2:
    xAddEventListener(vinculo, 'click', funcRef, false);
    Last edited by MikeFoster; Dec 11, 2006 at 11:20.

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried using DOM0:
    vinculo.onclick = funcRef;
    but it does not work for FF.

    The problem using DOM2, xAddEventListener, is that I want to try it (onclick event) as any other property.

  5. #5
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pacctono View Post
    I tried using DOM0:
    vinculo.onclick = funcRef;
    but it does not work for FF.
    That technique should work in almost every browser. Give us a link to your page and we'll take a look.

  6. #6
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    oops my code was messed up here let me fix it...
    Code:
    var aElement = document.createElement('a');
    aElement.setAttribute('onclick', Function('buscarSolucion(1,1,1)'));
    aElement.innerHTML = 'SomeLink';
    aElement.setAttribute('href', 'somelocation.htm');
    document.body.appendChild(aElement);
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, Mike, my page is on an intranet, but taking the original code:

    var vinculo = xCreateElement('a');
    var nodoTexto = document.createTextNode('texto');
    xAppendChild(vinculo, nodoTexto);
    xAppendChild(celda2, vinculo);
    var selcodact1 = "buscarSolucion(1,1,1)";
    vinculo.setAttribute('onclick', selcodact1);

    I think the whole problem is on line 5, with 'var selcodact1', that contains the function that I want to execute dinamically on the onclick event.

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,608
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    The last line is coded in such a way that it expects selcodact1 to be the function to be called but you have it defined as a text string and not a function.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    felgall is right. You can't pass arguments to the listener in that way. Here's a way to do it using a closure to remember the arguments:
    Code:
    function bindEventListener(ele, sEvent, fnListener, bCap, arg1, arg2)
    {
      xAddEventListener(ele, sEvent,
        function() {
          // in this function you have access to arg1, arg2, etc.
        }, bCap
      );
    }

  10. #10
    I'm not a human
    Join Date
    Aug 2006
    Location
    India
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,


    the setattribute for "onclick" will work on FF but not on IE.

    i think u can create an id for the table cell and then try to get the element using the id.


    like this
    var vinculo = xCreateElement('a');
    vinculo.setAttribute('id', 'tcell');
    var e = document.getElementById('tcell');
    e.onclick = IncrementQuantity;

    Try this i think it would work.
    Regards,
    Vijay
    Follow me on twitter @vijaycbe
    World Holiday Calander || My Cricket Blog

  11. #11
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It worked, thanks.

    Quote Originally Posted by MikeFoster View Post
    felgall is right. You can't pass arguments to the listener in that way. Here's a way to do it using a closure to remember the arguments:
    Code:
    function bindEventListener(ele, sEvent, fnListener, bCap, arg1, arg2)
    {
      xAddEventListener(ele, sEvent,
        function() {
          // in this function you have access to arg1, arg2, etc.
        }, bCap
      );
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •