Results 1 to 4 of 4

Thread: The most basic question you will ever see. Regarding "this".

  1. #1
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default The most basic question you will ever see. Regarding "this".

    Can someone tell me what is going wrong in this example?

    JS:

    Code:
    function hideMe() {
    	this.style.display="none";
    }
    HTML:

    Code:
    <a href="javascript:void(0);" onclick="hideMe();">
     	Some text.
    </a>
    I get the error "this.style is null or not an object" in IE6, and "this.style has no properties" in FF2. Shouldn't "this" in this instance be referencing the anchor tag?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This reminds me of the now infamous and probably mostly forgotten quote:

    "That depends upon what the meaning of 'is' is."

    Anyways, no. In a free standing function like that, this refers to the window.

    You could:

    Code:
    function hideMe(el) {
    	el.style.display="none";
    }
    and:

    HTML Code:
    <a href="javascript:void(0);" onclick="hideMe(this);return false;">
     	Some text.
    </a>
    There are other ways to assign functions as events to elements and, depending upon the browser and the method used, your original syntax would actually work.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    jlizarraga (06-23-2008)

  4. #3
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default

    Thanks!

  5. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    You could:

    Code:
    function hideMe(el) {
    	el.style.display="none";
    }
    and:

    HTML Code:
    <a href="javascript:void(0);" onclick="hideMe(this);return false;">
     	Some text.
    </a>
    There are other ways to assign functions as events to elements and, depending upon the browser and the method used, your original syntax would actually work.
    While this solution works, its really overlapping the design with the dynamics. Another solution that would work would be to use the javascript to assign/re-assign the a class/id to this element which would use that class/id styles which state this element and its child parts as "obsolete" for the moment.

    Code:
    <script type="text/javascript">
    function hideEl(el) {
        el.setAttribute('class', 'className');
    }
    </script>
    
    <style type="text/css">
    .className {display:none}
    </style>
    
    <p><a href="" onclick="hideMe(this)">View/Hide</a></p>
    that would work too, however at the same time you are still writing inline javascript, so depending on how many times you had this appear within the page and/or document it might be better to take out the javascript and use full obtrusive javascript, separating the content layer from the design layer

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
  •