admin管理员组文章数量:1025207
Trying to get the id or name of the button that has been clicked. But I only get an undefined message in the popup when I try to access the id or name of the button. Could someone point me in the right direction to get this working?
<!DOCTYPE html PUBLIC "-//W2C//DTD HTML 4.01//EN" ".dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing : Buttons</title>
<script src=".11.0/jquery.min.js"></script>
</head>
<div id="buttons" style="width:100%;height:100%">
<p id="custom_buttons" >
</p>
</div>
<div id="footer">
</div>
<script type="text/javascript">
$(function() {
var datasetPresets = ["Python", "Java", "C++"];
var datasetPresetsContainer = $("#custom_buttons");
$.each(datasetPresets, function(index, value) {
datasetPresetsContainer.append("<button class=std_buttons value=" + value + ">" + value + "</button>");
});
$("button.std_buttons").click(function () {
var button = $(this);
alert(button.id);
});
});
</script>
</body>
</html>
</html
Trying to get the id or name of the button that has been clicked. But I only get an undefined message in the popup when I try to access the id or name of the button. Could someone point me in the right direction to get this working?
<!DOCTYPE html PUBLIC "-//W2C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing : Buttons</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<div id="buttons" style="width:100%;height:100%">
<p id="custom_buttons" >
</p>
</div>
<div id="footer">
</div>
<script type="text/javascript">
$(function() {
var datasetPresets = ["Python", "Java", "C++"];
var datasetPresetsContainer = $("#custom_buttons");
$.each(datasetPresets, function(index, value) {
datasetPresetsContainer.append("<button class=std_buttons value=" + value + ">" + value + "</button>");
});
$("button.std_buttons").click(function () {
var button = $(this);
alert(button.id);
});
});
</script>
</body>
</html>
</html
Share
Improve this question
asked Mar 14, 2014 at 7:19
Ørjan PettersenØrjan Pettersen
331 gold badge1 silver badge4 bronze badges
3
|
4 Answers
Reset to default 7Basically, try
alert(this.id);
instead of
alert(button.id);
But this question shows a minimum research effort, because it has been answered multiple times here, and a simple search would suffice:
How to get ID of button user just clicked?
Getting the ID of the element that fired an event
Get id of element on button click using jquery
how to get the id when a certain button is clicked using jquery
In pure javascript, you would do something like this
$("button.std_buttons").click(function (event) {
var button = event.target;
alert(button.id);
});
read more about event object here https://developer.mozilla.org/en/docs/Web/API/Event
You need to use attr()
to get the id
of your button because button
is a jQuery object:
alert(button.attr('id');
or you can keep your selector but do not convert it to jQuery object:
var button = this;
alert(button.id);
you just use attr(), the properties are accessed using the attr("property name") in jquery
button.attr('id');
or just
$(this).id
Trying to get the id or name of the button that has been clicked. But I only get an undefined message in the popup when I try to access the id or name of the button. Could someone point me in the right direction to get this working?
<!DOCTYPE html PUBLIC "-//W2C//DTD HTML 4.01//EN" ".dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing : Buttons</title>
<script src=".11.0/jquery.min.js"></script>
</head>
<div id="buttons" style="width:100%;height:100%">
<p id="custom_buttons" >
</p>
</div>
<div id="footer">
</div>
<script type="text/javascript">
$(function() {
var datasetPresets = ["Python", "Java", "C++"];
var datasetPresetsContainer = $("#custom_buttons");
$.each(datasetPresets, function(index, value) {
datasetPresetsContainer.append("<button class=std_buttons value=" + value + ">" + value + "</button>");
});
$("button.std_buttons").click(function () {
var button = $(this);
alert(button.id);
});
});
</script>
</body>
</html>
</html
Trying to get the id or name of the button that has been clicked. But I only get an undefined message in the popup when I try to access the id or name of the button. Could someone point me in the right direction to get this working?
<!DOCTYPE html PUBLIC "-//W2C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing : Buttons</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<div id="buttons" style="width:100%;height:100%">
<p id="custom_buttons" >
</p>
</div>
<div id="footer">
</div>
<script type="text/javascript">
$(function() {
var datasetPresets = ["Python", "Java", "C++"];
var datasetPresetsContainer = $("#custom_buttons");
$.each(datasetPresets, function(index, value) {
datasetPresetsContainer.append("<button class=std_buttons value=" + value + ">" + value + "</button>");
});
$("button.std_buttons").click(function () {
var button = $(this);
alert(button.id);
});
});
</script>
</body>
</html>
</html
Share
Improve this question
asked Mar 14, 2014 at 7:19
Ørjan PettersenØrjan Pettersen
331 gold badge1 silver badge4 bronze badges
3
-
3
alert(this.id)
will do. – elclanrs Commented Mar 14, 2014 at 7:21 - alert(button.attr('id')); – Alex Mathew Commented Mar 14, 2014 at 7:27
- Thanks to the answers here, I realized that my variable was not named "id", but "value". So, now it works. Thanks. – Ørjan Pettersen Commented Mar 15, 2014 at 8:34
4 Answers
Reset to default 7Basically, try
alert(this.id);
instead of
alert(button.id);
But this question shows a minimum research effort, because it has been answered multiple times here, and a simple search would suffice:
How to get ID of button user just clicked?
Getting the ID of the element that fired an event
Get id of element on button click using jquery
how to get the id when a certain button is clicked using jquery
In pure javascript, you would do something like this
$("button.std_buttons").click(function (event) {
var button = event.target;
alert(button.id);
});
read more about event object here https://developer.mozilla.org/en/docs/Web/API/Event
You need to use attr()
to get the id
of your button because button
is a jQuery object:
alert(button.attr('id');
or you can keep your selector but do not convert it to jQuery object:
var button = this;
alert(button.id);
you just use attr(), the properties are accessed using the attr("property name") in jquery
button.attr('id');
or just
$(this).id
本文标签: jQueryJavascriptget the name of a clicked buttonStack Overflow
版权声明:本文标题:jquery - Javascript, get the name of a clicked button - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1739408231a1649342.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
alert(this.id)
will do. – elclanrs Commented Mar 14, 2014 at 7:21