tags:

views:

33

answers:

3

I want to associate a click handler with a class and then hide the element associated with the particular id that was clicked:

$(".myclass").click(function()
{
    $("#myclass_123").hide();
    return false;
});

Obviously the code above doesn't work because it doesn't calculate the "_123" part.

The ids in the class have the same name as the class that they are associated with, but they also have an underscore and a number attached to the end:

Would appreciate any help identifying the id to target.

+1  A: 

How about:

$(".myclass").click(function() {
    $(this).hide();
    return false;
});
Justin Gallagher
+1  A: 

Use $(this) to get the element clicked on

$(".myclass").click(function()
{
    $(this).hide();
    return false;
});
Eric
To be crystal clear, in the anonymous callback function `this` is the DOM element that was clicked. By using `$()` you get a jQuery object wrapping that DOM element allowing you to do jQuery stuff on it, hide() in this case.
joshperry
A: 

if the element is a anchor tag, you need a stopPropagation. otherwise you just need to hide the element.

$(".myclass").click(function(event) {
    event.stopPropagation();
    $(this).hide();
});

if you want all the other elements are seen can also be shown in this way.

$(".myclass").click(function(event) {
    event.stopPropagation();
    $(".myclass").show();
    $(this).hide();
});
andres descalzo