views:

63

answers:

5

Hi,

I have a that contains a link with class 'remove-project' that I want to default to hidden and set to visible when the div is hovered. So far I have this (which doesn't work)

$('.project-container').hover(function() {
    $(.remove-project).show();
},
function() {
    $(.remove-project).hide();
});


<?php
    foreach($user['Project'] as $project)
    {
        echo '<div class=project-container>';
        echo $html->link($project['title'], array('controller' => 'projects', 'action' => 'view', $project['slug'])).' <small>Posted '.$time->niceShort($project['created']).'&nbsp;</small><a href=
        class=remove-project>Delete</a>';
        echo '<br />';
        echo strip_tags($text->truncate(
                $project['description'], 
                400,
                array(
                    'ending' => '...', 
                    'exact' => false,
                                            'html' => false
                )));

        echo '<br /><br />';
        echo '<b>Tags</b>: '.$project['tags'];
        echo '</div>';
        echo '<br /><br />';

    }
?>

I think I'm going wrong with

$(.remove-project).show();

Can someon help me out?

+4  A: 

I think you need to do:

$('.remove-project').show();

(AKA, use quotes to perform the selection correctly). This is assuming, of course, that component is already in your HTML and is hidden.

Update: To ensure that the div tag is already in your HTML and hidden by default you can do:

<div class="remove-project" style="display:none;">

Of course, it is recommended that you don't embed the style directly but apply a class to your div. But...this will work.

JasCav
thanks, how do i default to hidden?
iamjonesy
<div class="remove-project" style="display:none;">
hookedonwinter
@Jonesy - What @hookedonwinter said. (Thank you.) I'll update my answer to reflect that.
JasCav
Keep in mind that if you don't set the context of your selector, you'll be `show` ing/`hide` ing all elements with a `remove-project` class ...
EAMann
There are multiple '.remove-project' elements so you actually need $('.remove-project', this).show(); as a few answers below have stated.
Plaudit Design - Web Design
+3  A: 

You need quotes around the selector:

$('.remove-project').show();
hookedonwinter
+1  A: 

As others noted, you need to quote your selector.

Additionally, you can simplify your code a bit by using jQuery's .toggle() method:

$('.project-container').hover(function( e ) {
    $('.remove-project').toggle( e.type === 'mouseenter' );
});

The single function will get fired for both mouseenter and mouseleave, and toggle will show when e.type === "mouseenter", otherwise will hide.

patrick dw
A: 

Your selector needs quotes. If you use Firefox install Firebug. It has a nice console to debug. The javascript error should have print out in the console for the code you have. Also you should be quote attribute in html. Last since their are multiple links with 'remove-project' class you need to specify a context of your jquery selector. Here is the new code

$('.project-container').hover(function() {
    $('.remove-project', this).show();
},
function() {
    $('.remove-project', this).hide();
});


<?php
    foreach($user['Project'] as $project)
    {
        echo '<div class="project-container">';
        echo $html->link($project['title'], array('controller' => 'projects', 'action' => 'view', $project['slug'])).' <small>Posted '.$time->niceShort($project['created']).'&nbsp;</small><a href=
        class=remove-project>Delete</a>';
        echo '<br />';
        echo strip_tags($text->truncate(
                $project['description'], 
                400,
                array(
                    'ending' => '...', 
                    'exact' => false,
                                            'html' => false
                )));

        echo '<br /><br />';
        echo '<b>Tags</b>: '.$project['tags'];
        echo '</div>';
        echo '<br /><br />';

    }
?>
Plaudit Design - Web Design
A: 

I wouldn't use the show and hide methods because they're a bit slow (particularly if you're using a lot of animation on the page). Instead, I'd use a child selector so you're only working with that <div>'s link and add/remove a class of "hidden" that sets the display property to none.

So ...

$('.project-container').mouseenter(function() {
    $('.remove-project', this).removeClass('hidden');
}).mouseleave(function() {
    $('.remove-project', this).addClass('hidden');
});

When the mouse enters a <div>, the "hidden" class will be removed from that <div>'s link. When the mouse leaves, it will be re-added.

EAMann
The show and hide methods are slow? They certainly seem as though they would be faster than the work jQuery has to do when adding and removing classes.
patrick dw
Both `show()` and `hide()` can iterate through animations. It's very easy to accidentally turn a simple visibility toggle into a fade with a typo - also, if you have a `stop()` call in the same script that's set to terminate other animations, it will prevent `show()` and `hide()` from firing at all.
EAMann
Ok, I see what you're saying, although I think it would be an unusual typo, and if you happen to have jQueryUI loaded, you could make the same typo with `addClass/removeClass`. Not that I'm opposed to styling via classes though. It's certainly a better approach. But for simple show/hide, I do personally prefer the methods.
patrick dw
The only reason I pointed it out was because it's a mistake I actually made last week with some scripts. I was using show/hide in places meant to be instantaneous and in places meant to be animated ... while cleaning my code, I went through and re-added a 1000 ms delay to each, causing my instantaneous show/hides to fail. So unusual, yes, but if it's a mistake I've made, I'm sure someone else is bound to in the future and I'll do what I can to warn them against it.
EAMann