+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Help on binding mouseover with jquery.

  1. #1
    Teensweb is offline x10 Lieutenant Teensweb is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    352

    Help on binding mouseover with jquery.

    Alright, the problem I face now is simple and silly: I need to make some elements show up when another element is hovered, well this is simple, to understand where my problem arises, take a look at the below code:
    Code:
    function builder(){
    ....
    $(myelement).bind('mouseenter',function(){el.slideout()});
    ....
    }
    Here, slideout() is a well defined custom function for a class of which el is a member.
    Now, the problem is, the function builder is called everytime another parent and child system is built, so the el (which is a global variable) changes on each call of the function which means I lose the link between my previous parent and child. Is there any possible way to overcome this problem?
    I guess(but I am not sure) that the problem can be solved if el is declared as a local variable of builder() but then, how would I access that variable inside the function(){} in the bind?

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Help on binding mouseover with jquery.

    Insufficient information to make a meaningful suggestion.
    Nothing is always absolutely so.

  3. #3
    Teensweb is offline x10 Lieutenant Teensweb is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    352

    Re: Help on binding mouseover with jquery.

    I'll elaborate- el is an object and I want to access one its member functions(namely slideout) from the function(){} block in my above code.
    I thought I could overcome the problem by assigning el to a global variable but since el changes everytime the parent function is called, the glocal changes too,so I can't attach a establish relation between an element and its parent.
    Last edited by Teensweb; 05-19-2011 at 12:15 PM.

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Help on binding mouseover with jquery.

    What you need is a closure.
    Nothing is always absolutely so.

  5. #5
    Teensweb is offline x10 Lieutenant Teensweb is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    352

    Help on binding mouseover with jquery.

    Oh thanx, I worked it out using a closure but is it the only way to work around the prob?

  6. #6
    Teensweb is offline x10 Lieutenant Teensweb is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    352

    Re: Help on binding mouseover with jquery.

    BTW, can someone tell me how to bind an event to multiple objects through a for loop?
    At first I tried going simple like:
    Code:
    function somefunc(){
    var id;
    for(i=0;i<10;i++)
    $("#"+id+i).bind('mouseleave', function(){});
    }
    And after realizing that the bind worked only for the last element of the loop(which obviously is not what I want), I changed it to
    Code:
    function mbind(sId){
      return function(){
      $("#"+sId).bind('mouseleave', function(){});}
    }
    function somefunc(){
    var id;
    for(i=0;i<10;i++)
    (mbind(id+i))();  
    }
    expecting it would now work for all elements in the loop, but hell no!
    Can anyone help me with this?
    Last edited by Teensweb; 05-21-2011 at 06:45 AM.

  7. #7
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Help on binding mouseover with jquery.

    The value of id is undefined in both cases.
    Nothing is always absolutely so.

  8. #8
    Teensweb is offline x10 Lieutenant Teensweb is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    352

    Re: Help on binding mouseover with jquery.

    Come on dude, don't be so obtuse! Do you really think that I would play with an undefined variable to make such a fuss? I skipped the defining part there since that's not where my problem arises. What I am still asking is: how to bind the event to all elements on the loop?
    Last edited by Teensweb; 05-21-2011 at 11:53 AM.

  9. #9
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Help on binding mouseover with jquery.

    Dude, if you don't post your complete code, nobody can tell where you are going wrong.
    Nothing is always absolutely so.

  10. #10
    Teensweb is offline x10 Lieutenant Teensweb is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    352

    Re: Help on binding mouseover with jquery.

    Alright then consider that the statement: var id;
    is replaced by: var id="sl_";
    in both of my above code blocks.

+ Reply to Thread
Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers