Yasir
Yasir

Reputation: 3907

How can I get the id of clicked element using jquery?

I want to get the id of the clicked element and then show divs that match this id. I am using the following code, and it's not working. Please help.

$(function () {  

var tabContainers = $('div.difContetform > div');
    $('div#head-nav ul a').click(function (event) { 
        $('div#head-nav ul a').removeClass('current');
        $(this).addClass('current');
        var current_id = $(this).attr("id");
        var targeted='DIV'+current_id;
        $(targeted).show();
        $(targeted:not).hide(); 
        // 
        return false; 
     })
});

Upvotes: 3

Views: 18912

Answers (2)

Yasir
Yasir

Reputation: 3907

Thanks a lot dear now i am able to show the div but could't hide others. as you said all page disappear i have on container id=formContainer and other divs (child of that id div) under this id show hide and 1 shown that is clicked.

i am using below syntax

$('div#difContetform > div:not(#'+targeted+')').hide();

but its not working although page not disappear but not hiding other divs

Upvotes: 1

Adam Bellaire
Adam Bellaire

Reputation: 110429

You want to use the right selector syntax to grab your divs by id, which is the string #id... Therefore:

 $('#'+targeted).show();
 $('something:not(#'+targeted+')').hide();    

EDIT: Looking at this again (double-take), you can't just hide everything that doesn't match, as it will hide your whole page. You'll need to make sure you're selecting only DIVs, but not the one you want to show. How that works depends on your page layout (hence the something in the example above).

Upvotes: 7

Related Questions