SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Jan 2005
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    pass itself as an object to function

    how could i pass a self object into a function and then use it?
    like now, i have this:
    Code:
    <script>
    function alertme(divid) {
    	alert(divid.id);
    }
    </script>
    
    <div id="testid" name="testname"><a name="linkname" href="javascript:alertme(parent)">text link</a></div>
    so my idea was like if i could pass the parent of the link, which is the div object into the function call alertme and show the id of the div object. however this didn't seem to work. i got an empty alert box.

    i have tried this, self and top instead of parent but all of those don't work either. moreover, i thought self may refer to the anchor tag object itself.

    my idea doing this is so that i can save trouble going through the document.getElementById('testid').style..... which is very long. and having it able to pass itself into the function could make the function more dynamic and cater for not just one div but other div as well.

    thanks!

  2. #2
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm afraid you can't do that, because it considers Window to be the parent object.

    Just type this code:
    Code:
    function alertme(divid) {
        alert(divid);
    }
    And you will see.


    Use document.getElementById()

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "this" is not passed to "javascript:" hrefs, try using event handler instead:

    Code:
    <div id="testid" name="testname"><a name="linkname" href="#"
           onclick="alertme(parentNode)">text link</a></div>

  4. #4
    SitePoint Addict dAEk's Avatar
    Join Date
    Oct 2003
    Location
    Stockholm, Sweden
    Posts
    224
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    "this" is not passed to "javascript:" hrefs
    How come/why not?
    David Shamloo-Ekblad

    Go Habs! | For music addicts: Last.fm, SongMeanings

  5. #5
    SitePoint Addict mmanders's Avatar
    Join Date
    Jul 2006
    Location
    Edinburgh, Scotland
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "javascript:" urls have remained in the javascript specification for a long time, which has a lot of history. They are considered deprecated because "javascript:" is not a protocol for which an RFC exists, and as such should not be used an anchor element which expects some sort of protocol (or http by default).


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
  •