admin管理员组文章数量:1026989
I want to get an id as a string of a div that was clicked. is it possible to do?
HTML
<div id="test">
<div class='button' id="a" ><p>Click 1</p></div>
<div class='button' id="b" ><p>Click 2</p></div>
<div class='button' id="c" ><p>Click 3</p></div>
<div class='button' id="d" ><p>Click 4</p></div>
</div>
I can do something like this:
$("#a").click(function() {
/* do some action with this div */
});
But I want to detect the id of just clicked div, because I will have a lot of divs like this, and it doesn't make sense to rewrite the same code over and over again.
I want to get an id as a string of a div that was clicked. is it possible to do?
HTML
<div id="test">
<div class='button' id="a" ><p>Click 1</p></div>
<div class='button' id="b" ><p>Click 2</p></div>
<div class='button' id="c" ><p>Click 3</p></div>
<div class='button' id="d" ><p>Click 4</p></div>
</div>
I can do something like this:
$("#a").click(function() {
/* do some action with this div */
});
But I want to detect the id of just clicked div, because I will have a lot of divs like this, and it doesn't make sense to rewrite the same code over and over again.
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jan 22, 2013 at 21:38 VorVor 35.3k46 gold badges141 silver badges196 bronze badges 2- 1 Although your HTML is fine, I think I'd choose another class name for your divs: IMO, 'button' can be confusing. – frenchie Commented Jan 22, 2013 at 21:41
- 1 Also, I don't think you need to have a <p> tag inside your divs; just put the text directly inside the div. As a general rule, when you have a box inside a box (think of tags as boxes) you can probably simplify the HTML by removing the inner box and apply the css of the inner box to the outter box. – frenchie Commented Jan 22, 2013 at 22:09
3 Answers
Reset to default 5$("#a").click(function() {
var id = this.id;
});
If you need to set click
event for all .button
elements, here is the short way:
$(".button").click(function(e) {
var id = this.id; // or e.target.id;
});
$("div.button").click(function() {
console.log(this.id);
});
$(this).attr('id')
will get you the id, But if you want to access the div you can use
$(this).parent('div')
this will only return if the immediate parent is a div, if want to find up you can use
$(this).parents('div')
I want to get an id as a string of a div that was clicked. is it possible to do?
HTML
<div id="test">
<div class='button' id="a" ><p>Click 1</p></div>
<div class='button' id="b" ><p>Click 2</p></div>
<div class='button' id="c" ><p>Click 3</p></div>
<div class='button' id="d" ><p>Click 4</p></div>
</div>
I can do something like this:
$("#a").click(function() {
/* do some action with this div */
});
But I want to detect the id of just clicked div, because I will have a lot of divs like this, and it doesn't make sense to rewrite the same code over and over again.
I want to get an id as a string of a div that was clicked. is it possible to do?
HTML
<div id="test">
<div class='button' id="a" ><p>Click 1</p></div>
<div class='button' id="b" ><p>Click 2</p></div>
<div class='button' id="c" ><p>Click 3</p></div>
<div class='button' id="d" ><p>Click 4</p></div>
</div>
I can do something like this:
$("#a").click(function() {
/* do some action with this div */
});
But I want to detect the id of just clicked div, because I will have a lot of divs like this, and it doesn't make sense to rewrite the same code over and over again.
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jan 22, 2013 at 21:38 VorVor 35.3k46 gold badges141 silver badges196 bronze badges 2- 1 Although your HTML is fine, I think I'd choose another class name for your divs: IMO, 'button' can be confusing. – frenchie Commented Jan 22, 2013 at 21:41
- 1 Also, I don't think you need to have a <p> tag inside your divs; just put the text directly inside the div. As a general rule, when you have a box inside a box (think of tags as boxes) you can probably simplify the HTML by removing the inner box and apply the css of the inner box to the outter box. – frenchie Commented Jan 22, 2013 at 22:09
3 Answers
Reset to default 5$("#a").click(function() {
var id = this.id;
});
If you need to set click
event for all .button
elements, here is the short way:
$(".button").click(function(e) {
var id = this.id; // or e.target.id;
});
$("div.button").click(function() {
console.log(this.id);
});
$(this).attr('id')
will get you the id, But if you want to access the div you can use
$(this).parent('div')
this will only return if the immediate parent is a div, if want to find up you can use
$(this).parents('div')
本文标签: javascripthow to get en id of clicked element jqueryStack Overflow
版权声明:本文标题:javascript - how to get en id of clicked element jquery - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745568391a2156587.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论