tags:

views:

33

answers:

1

hello, I'm using hoverIntent to fire onMouseOver and onMouseOut events

it works for one of my li, actually it looks like it appends itself to only one li and as I hover other list items it displays the show() and hide() functions to the last list item.

here is my code:

            <script>
                $(document).ready(function(){
                        var config = {
                            over: show, // function = onMouseOver callback (REQUIRED)    
                            timeout: 200, // number = milliseconds delay before onMouseOut    
                            out: hide // function = onMouseOut callback (REQUIRED)    
                        };
                    $("#ULid li").hoverIntent( config )
                    $('a.names').click(function() {
                        var id = this.id.replace('id', '');
                        $.get("dependencies/ajax/contacts.ajax.php",{
                            cmd    : "view",
                            id   : id,
                        }, function(data) {
                            $("#detailsPane").fadeIn(500).html(data);
                        });
                    });
                    $('a.delete').click(function() {
                        var id = this.id.replace('id', '');
                        $.get("dependencies/ajax/contacts.ajax.php",{
                            cmd    : "view",
                            id   : id,
                        }, function(data) {
                            $("#contact-" + id).slideUp("slow").html(data);
                        });
                    });
                });
                function show(){
                    var selector = $(this).find('.delete-div');
                    selector.fadeIn('slow');
                }
                function hide(){
                    var selector = $(this).find('.delete-div');
                    selector.fadeOut(500);
                }
            </script>
            <div class="toolbar">
                <a href="javascript:void(0);" onclick='$("#addC").toggle();'><p id="addC20" style="float: left"></p></a>
                <p style="float: right; position: relative; top: 0px; left: -10px;">Contact Manager</p>
            </div><!-- end toolbar -->

            <div id="contact-wrap">
                <div id="groups">
                    <div id="contact-search" class="search">
                        <input type="text" class="input" name="search" />
                        <img src="dependencies/images/header/search.png" />
                    </div><!-- end search -->

                    <h3 id="groups-heading">Groups</h3>
                    <ul class="post">
                    <? while($row = mysql_fetch_array($groups)) {
                        echo '<li><a href="#" class="groups" id="' . $row['id'] . '">' . $row['first_name'] . ', ' . $row['last_name'] . '</a></li>';
                    } ?>
                        <li>Business</li>
                        <li>Personal</li>
                        <li><a href="#">Add group</a></li>
                    </ul><!-- end post -->
                </div><!-- end names -->

                <div id="names">
                    <h3 id="names-heading">Contacts</h3>
                    <ul class="post" id="ULid">
                    <? while($row = mysql_fetch_array($contacts)) { ?>
                        <li id="contact-<?=$row['id'];?>">
                            <p class="contact-image" style="float: left;"></p>
                            <span><a href="javascript:void(0);" class="names" id="<?=$row['id'];?>"><?=$row['first_name'];?>, <?=$row['last_name'];?></a></span>
                            <div class="delete-div" style="float: right; display: none;">
                                <a href="javascript:void(0);" class="delete" id="<?=$row['id'];?>">
                                    <p class="delete-image" style="float: left;"></p>
                                </a>
                            </div>
                        </li>
                    <? } ?>
                    </ul><!-- end post -->
                </div><!-- end names -->

                <div id="detailsPane"></div>

If I move the script outside the while loop then it show the delete div to the first list item and only to that one as I hover over to other list items.

Any ideas as to why it's doing this?

+1  A: 

hmmmm.... you really need some help there!

firstly, let's do this outside the loop ,

<script>
$(document).ready(function(){
    var config = {  over: show,    
    timeout: 200,    
    out: hide    
    };
    $('#ULid li').hoverIntent( config );    

    $('a.names').click(function(){
        var id = this.id.replace('id','');
        $.get("dependencies/ajax/contacts.ajax.php",{
            cmd    : "view",
            id   : id,
    }, function(data) {                                      
               $("#detailsPane").fadeIn().html(data);
        });
    });

    $('a.delete').click(function() {

       var id = this.id.replace('id', '');
       var $this = $(this);

       $.get("dependencies/ajax/contacts.ajax.php",{
          cmd    : "delete",
          id   : id,
       }, function(data) {
          $this.closest('li').slideUp("slow").html(data);
       });
    });


});

function show(){ 
    var selector = $(this).find('.delete');
    selector.fadeIn('slow');
}
function hide(){ 
    var selector = $(this).find('.delete');
    selector.fadeOut('slow');
}
</script>

then your php

<ul id="ULid">
<? while($row = mysql_fetch_array($contacts)) { ?>

    <li id="contact-<?=$row['id'];?>">
        <p class="contact-image" style="float: left;"></p>
        <span><a href="#" class="names" id="id<?=$row['id'];?>"><?=$row['first_name'];?>, <?=$row['last_name'];?></a></span>
        <div class="delete" style="float: right; display: none;">
            <a href="javascript:void(0);" class="view"onclick='
                $.get("dependencies/ajax/contacts.ajax.php",{
                    cmd: "delete",
                    id: "<?=$row['id'];?>"
                }, function(data){
                $("contact-<?=$row['id'];?>").fadeOut(2000).html(data);
                });
            '>
            <p class="delete-image" style="float: left;"></p>
            </a>
        </div>
    </li>
<? } ?>
</ul>
Reigel
haha, ya I had the script outside the loop and couldn't get it to work with more than one li so i thought i'd see if i'd work within the loop and just attach it each li via the id attribute. Give me a min to see if your edit helped
s2xi
haha - and may I ask if your [site](http://www.s2xi.net/) is right? seems not working for me. ;)
Reigel
oh, my dev site is offline but just a url for me to test files. The project I'm working on is helixagent.com where I'm trying out this bit of code. Also, I can't seem to get your version of the script to show the div on the mouse over event anymore. hmmm
s2xi
ok, on the `show` function, try `alert(selector)` and see if we got the right selector. and also I can't seem to figure out where's `.delete-<?=$row['id']; ?>` pointing to. Well, is it `<p class="delete-image" style="float: left;"></p>` ? but this one is `.delete-image`.
Reigel
oh the .delete-<?=$row['id']; ?> is the class of the div that is going to fadeIn and fadeOut. and the class="delete-image" is the icon that is showed to the user, its a little x instead of saying 'delete'
s2xi
ok, I'm seeing it now and so made an edit. I changed your `div` to have a class just `delete`. and the `show` and `hide` functions were changed too. please check. ;)
Reigel
if you want to see whats going on with the code you can log in as test/password. Its in the Contacts tab
s2xi
ahhh.. you can also see it working [here](http://jsfiddle.net/9ayb7/)
Reigel
Hmmm, I applied your edit, but like I thought. It shows ALL the div's with a class of delete. I originally wanted to only show the div that belongs to a specific id, that is why I had the <?$row['id'];?> in the class name so that the script (that i had in the while loop) could fire on that div only
s2xi
and so, this was your problem, `$("#ULid").hoverIntent( config )`. That should be, `$("#ULid li")` with the `li`. ;)
Reigel
haha, and so it was lol
s2xi
hey, I don't mean to steal you for another problem but its related to this lil project i'm trying to do lol. But in my other question at http://stackoverflow.com/questions/3866908/load-revelant-data-when-clicking-on-link-using-jquery I wanted to have the user click on a name and load data in the detailsPane from each contact. But I'm confused as to what to query or how the javascript comes into play. Can you give me a little help please?
s2xi
:D glad that's solve now.
Reigel
didn't that accepted answer solve it?
Reigel
hold on, needed a bathroom break lol. Yea I accept your answer ;-)
s2xi
no - I mean in this [load-revelant-data-when-clicking-on-link-using-jquery](http://stackoverflow.com/questions/3866908/load-revelant-data-when-clicking-on-link-using-jquery) - accepted answer did not solve it?
Reigel
oh, well seeing as how my question was javascript and he helped me out with the js portion i took his answer but really left with more questions on how the php side was suppose to handle the ajax request then on how to show output in my detailsPane div ;-(
s2xi
haha. Okay, I'm not really good at server-side scripts but I can help you with ajax. So, are you good at php? or I mean can you query the things needed without ajax involved? for example, display all the details based on the id given. like `some/url/detail.php?id=123`
Reigel
ya i can query and do php $_GET values without using js
s2xi
Okay, try this [fiddle](http://jsfiddle.net/vf58L/), ask me if you have some questions.
Reigel
[fiddle update](http://jsfiddle.net/vf58L/1/)
Reigel
hmmm, you know maybe what I'm not getting is where to place the code because I can understand that I do one query to show my contacts in the names div but when the user clicks on the contact name in english words what is the script/php page suppose to be doing? Click on Jon Dow > Ajax Request > Query table using $_GET['id'] > detailsPane div....This is what I'm stuck, how to show new query data in this div
s2xi
[another fiddle](http://jsfiddle.net/vf58L/4/). You're almost there. If you can display something like `<div>name: reigel</di>` by using the php then we got no problem.
Reigel
hmmm, cool. Just a dumb question but why do you have id: 'id=123' ?
s2xi
No it's not a dumb question. I purposely put that there to see if you'll ask me :D Well, with the `url` and `data`, this is just like calling `http://fiddle.jshell.net/9ayb7/show/?id=123`, for your php to do some scripting. You can now do `$_GET['id']` which in php will give you `123`. [another fiddle to get an idea with](http://jsfiddle.net/vf58L/5/). In [jsfiddle.net](http://jsfiddle.net), I can't do server-side scripting, so I can't really show an example. You can try it yourself.
Reigel
Well I accomplished one thing, I captured the click and sent the data which contains the ID of the contact to my ajax page. I was able to load something via Firebug, but just looking at the script still don't understand how its being appended. For started why do you need to have the $("</div>")? from there I'm guessing its going to find my <ul> and add the new data in the UL?
s2xi
ahh, i got it to work lol. I just appended (data) directly to my detailsPane div. But now I'm seeing that it loads the data in a bubble effect. (I think thats what its called). How can I whipe out the current data before loading in new data?
s2xi
ahh mine was because the page (url in the ajax) contains lots of tags like doctype which giving me errors if I just do `$(data)`. that's to fixed it. If you just have just the `ul` on the page, then `$(data)` is already good.
Reigel
to wipe it out, don't append it. use `.html()` instead of `.append()` ;)
Reigel
I updated what I have, is it right?
s2xi
oh ya, append = bad... html = good! ;-)
s2xi
Is it good to have my JS in each iteration of my links? I just didn't know how to capture the ID of the item I want to work with without using PHP within the WHILE loop
s2xi
you = good!! ;)
Reigel
hehe, do you have paypal by any chance? I can't help but feel really thankful. *sigh* now to figure out how to get my Groups feature to work ;)
s2xi
looping it all is not good, make your anchor for the names like this, `<a href="#" class="names" id="id<?=$row['id'];?>">...</a>` and I have updated my codes above for `$('a.names')`. take a look.
Reigel
in your css too, put `#detailsPane { float:left; width: 554px; }` ;)
Reigel
hmmm, ok styled it and everything, hey i was amazed as to how the js got the id from the id attr ;) i forgot that I can grab data from attributes with JS! also, with that same set up I tried to remove my delete button js out of the loop but its not working correct. the data is acting funny
s2xi
updated the code above to what I got now
s2xi
hehe. You still got a problem on your delete. I will help you again tomorrow. It's 5:30pm already here and I'm about to go out from work and go home. ;)
Reigel
heh, ok. Ya i figured it out tho. forgot to change the cmd to 'delete' um. but is the $("#contact-" + id) correct syntax?
s2xi
added `$('a.delete')` for you. look above my edit. - cheers!
Reigel
hey, just quick question. I have my 'create' which loads the new form into the detailsPane, how can I save the form from there? I tried a regular submit input but that don't really play nice to how it submits. Is there a way to use the same ajax style within the 'create' function that lets me also call the 'save' function?
s2xi
hey, what seems to be the problem?
Reigel
Oh um, well so I was able to get all my actions to work using ajax on my contacts.php. My problem is that when I load 'create' from the contacts.ajax.php It loads my add new contact form but from there I don't know how to save it.I create a save link but its not getting the data and posting it to the ajax page. I copied the same format for the 'delete' function but made it point to the 'save' of the ajax page. I tried to echo any out puts but I don't get any results.
s2xi
it might be better to help you via email. ;) [email protected] - mail me. I will delete this comment after you do. ;)
Reigel
ok, got the email coppied ;-)
s2xi