SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    setAttribute multiple instances

    Ok I know there is a problem with IE (I wont bother inserting a witty comment here) on handling

    setAttribute('onclick','doStuff');

    I have found

    yourAnchor.onclick = function() {
    // do what you want in here.
    }

    this works, but I the script I am working on is going through every DIV with a certain class and adding links to the div, I want the button to change something within the div where it is located so I need to pass the id of the parent div.

    I thought perhaps there was someway to find the id of the button pressed?

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Let's say divs is your collection of divs that you're cycling through.
    Code Javascript:
    for (var i = 0, j = divs.length; i < j; i++) {
      divs[i].onclick = doStuff;
    }
    function doStuff() {
      var divid = this.id;
      // ...
    }

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok that is very cool, not really heard of 'this' before. I worked it into my code as i was generating the links like:

    div.onclick = function() {
    var divid = this.id;
    alert(divid);
    }

    I now only have to find the correct sibling to effect. I thought IE was only gay when messing up your layouts.

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    this

    What do you mean find the correct sibling? Show us some sample HTML (not hundreds of lines).


    Also, IE is not gay. It is a miserable anachronism.

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is ok that was not a problem due to the way I add the links to the DOM it is just a case of:

    var divId = this.parentNode.firstChild.id;

    It is fairly backwards, it would have been much simpler to just add the ID of the div to an onClick statement on the button, but this method is probably more to the DOM standards.

    Thanks for your help

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, it is. Adding the onclick attribute to HTML in an inline fashion is very old-school and not the best way to do things.


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
  •