How to get ID of an element in jQuery, which is being clicked
I have a scenario, wherein, I have a table and in some columns I have some hidden divs. Now in one of these columns I have a Div which contains another hidden div. Now I can开发者_StackOverflow中文版not apply a generalised function which will just show all divs within a particular row or even within a particular column. I would like to have a function which would detect the id or class of element which is being clicked upon. How to go about this ?
do this, onclick event of the element:
<div id="div1" onclick="getId();"> </div>;
function getId(){
var currentId = $(this).attr('id');
}
Like this?
$(<selector>).click(function() {
id = $(this).attr('id');
});
You do it like this;
$("div").click(function(){
alert($(this).attr("id"));
});
$('div').bind('click', function(){ // Make this part as specific as it can be
console.log($(this).attr('id'));
});
You can make your initial selector a bit more restrictive because selecting all divs on a page and assigning an event handler to them is not such a good idea.
This will log the ID of the element that was clicked. You can then add additional logic as shown to handle the "show" functionality.
$('div').click(function(){
console.log($(this).attr('id');
//other logic here
});
$(".class").click(function(){
var clicked = $(this).attr('id');
});
could help
精彩评论