SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing a variable in hover

    In hover, the structure is
    Code:
    $("div.x2").hover(
       function(){
    	var a = 1;},
       function(){
           alert(a);}
    );
    If I want to access a that was set in the mouseover part of this structure, or I want to change the value of a from the second function, do I have to create a global variable that can be accessed by both or is there a way to pass this variable back and forth between the two functions since they are both within the hover event?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,096
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    I think I'd use .data() for that

    Code javascript:
    $("div.x2").hover(
       function() {
    	$(this).data('a', 1);
       },
       function() {
           alert( $(this).data('a') );
       }
    );
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect, thanks. I appreciate it.


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
  •