tags:

views:

27

answers:

1

I have a this set up

<ul>
<li>item 1</li>
</ul>

my js let me make them sortable and what not, one feature I have is that via ajax i can create a new list item on the fly. But whats interesting is that I have a hoverIntent letting me hover over the list item and a delete and edit icon appears letting me perform the intended actions.

My problem is that when I make a new list item on the fly the hoverIntent isn't applied to it. The code created is identical to the other list items, I know it works because when I refresh the page and reload the js then the hoverIntent kicks in and applies the hover effects to the new items created.

None of this works on the fly tho, how can I make this work?

my js for creating a new list item is:

var timestamp = 0;
    $('.new-group').click(function(e){

        // Only one group per 5 seconds is allowed:
        if((new Date()).getTime() - timestamp < 5000) return false;

        $.get("resources/ajax/groups.ajax.php",{
            action :'new',
            uid     : uid,
            text   :'New Group, Doubleclick to Edit.'
        },function(data){
            $(data).hide().appendTo('.grouplist').fadeIn();
            //GROUP.find(".groups-action-div").show('slow');
        });
        timestamp = (new Date()).getTime();
        e.preventDefault();
    });

I'm betting that in my success function is where I can make this work,

you can log in and see my problem at www.helixagent.com with login as test/password go into the Contacts tabs and click on the + icon to make a new group

+1  A: 

hoverIntent loads the effect on page load, meaning that when you create a new element, you've already executed the hoverIntent aspects, and it doesn't apply to the newly create element.

Basically, you just have to run hoverIntent again

I haven't tested it, but you'd need to do something like this:

var timestamp = 0;
$('.new-group').click(function(e){

    // Only one group per 5 seconds is allowed:
    if((new Date()).getTime() - timestamp < 5000) return false;

    $.get("resources/ajax/groups.ajax.php",{
        action :'new',
        uid     : uid,
        text   :'New Group, Doubleclick to Edit.'
    },function(data){
        $(data).hide().appendTo('.grouplist').fadeIn();
        //GROUP.find(".groups-action-div").show('slow');
    });
    timestamp = (new Date()).getTime();
    e.preventDefault();


    var config_names = {
        over: show_delete_names,
        timeout: 200,
        out: hide_delete_names
    };
    $(".contactlist li:last-child").hoverIntent( config_names );
});
Zurahn
my config_names is a function outside of the jQuery function. These will act as global functions be accessible anywhere?
s2xi
I modified it to this $(".grouplist li:last-child .groups-action-div").hoverIntent( config_names ); to be more specific, but it still doesn't execute the hoverItent code again.
s2xi
Yes, it being defined globally it should be accessibly elsewhere, provided it's defined before the function is run. I just added it in there for clarity's sake. I'm not able to play around with it now to see what works, but it is a matter of applying the `hoverIntent` after the new element has been added.
Zurahn
oh ;-) but I did get what you are trying to tell me no worries. If i added my functions before the jquery function, will it then be picked up when i execute my create new group function?
s2xi
No, anything within `$()` waits until the DOM is loaded to execute. Your function definitions don't have any effect (unless you're defining them within another function, of course). If I'm understanding you correctly.
Zurahn
yup, that actually gave me alot of insight, didn't know thats what the $ represented.
s2xi
`$` is an alias to the jQuery function. That makes it a lot easier to look up the documentation - http://api.jquery.com/jQuery/
Zurahn
ah thank you, you helped me solve my issue. Thanks alot!
s2xi
Excellent, glad to help. Good luck with your site :)
Zurahn