admin管理员组文章数量:1416642
I'm attempting to simply get the id of a clicked button and use alert to display it. The code below trys to addEventListener
, if not falls back to attachEvent
. To get the ID, Im trying to pass this.id
as a parameter to myFunction
. but this is not working. I've seen plenty of great solutions using Jquery and the likes, but unfortunately I have to use only plain JS for this. How do I get the ID of a clicked button and store it in a variable to be displayed?
function addEvents()
{
var buttonArray=document.getElementsByClassName('mainButton');
for(i=0; i < buttonArray.length; i++)
{
if (document.addEventListener) {
buttonArray[i].addEventListener("click", myFunction(this.id));
} else if (document.attachEvent) {
buttonArray[i].attachEvent("onclick", myFunction(this.id));
}
function myFunction(clickedId) {
alert("Button" + clickedId + "was clicked.");
}
}
}
I'm attempting to simply get the id of a clicked button and use alert to display it. The code below trys to addEventListener
, if not falls back to attachEvent
. To get the ID, Im trying to pass this.id
as a parameter to myFunction
. but this is not working. I've seen plenty of great solutions using Jquery and the likes, but unfortunately I have to use only plain JS for this. How do I get the ID of a clicked button and store it in a variable to be displayed?
function addEvents()
{
var buttonArray=document.getElementsByClassName('mainButton');
for(i=0; i < buttonArray.length; i++)
{
if (document.addEventListener) {
buttonArray[i].addEventListener("click", myFunction(this.id));
} else if (document.attachEvent) {
buttonArray[i].attachEvent("onclick", myFunction(this.id));
}
function myFunction(clickedId) {
alert("Button" + clickedId + "was clicked.");
}
}
}
Share
Improve this question
asked Feb 3, 2015 at 2:46
exeleonexeleon
1211 gold badge2 silver badges14 bronze badges
1
- Does this answer your q?: stackoverflow./questions/4825295/… – Q A Commented Feb 3, 2015 at 2:55
4 Answers
Reset to default 5Actually you didn't need to pass an argument to myFunction
. That is a callback function and you can use the event variable.
function addEvents()
{
var buttonArray = document.getElementsByClassName('mainButton');
for(i = 0; i < buttonArray.length; i++)
{
if (document.addEventListener) {
buttonArray[i].addEventListener("click", myFunction);
}
else {
if (document.attachEvent) {
buttonArray[i].attachEvent("onclick", myFunction);
}
}
}
}
function myFunction(e) {
alert("Button " + e.target.id + " was clicked.");
}
Here is the jsfiddle.
You need to modify your code as follows:
function addEvents()
{
var buttonArray = document.getElementsByClassName('someclass');
for(i=0; i < buttonArray.length; i++)
{
if (document.addEventListener) {
buttonArray[i].addEventListener("click", myFunction);
} else if (document.attachEvent) {
buttonArray[i].attachEvent("onclick", myFunction);
}
function myFunction(e) {
alert("Button" + e.target.id + "was clicked.");
}
}
}
myFunction is the callback function, by using myFunction(id) you were calling the function thus defeating the purpose of the callback.
Currently, this code loops through the mainButton buttons on the page, and for each button, calls myFunction and sets the event listener to be the return value of myFunction. Since myFunction returns nothing, then the event listener also gets set to nothing. Probably what you are looking for, instead, is something like this:
if (document.addEventListener) {
buttonArray[i].addEventListener("click",
function() { myFunction(this.id); });
}
else if (document.attachEvent) {
buttonArray[i].attachEvent("onclick",
function() { myFunction(this.id); });
}
It would also probably be preferrable to move the declaration of the myFunction to be outside the for loop.
Try this:
Element.prototype.addEvent = function(eventName, callFunction) {
if (this.addEventListener) this.addEventListener(eventName.substring(2), callFunction, false);
else if (this.attachEvent) this.attachEvent(eventName, callFunction);
};
function clicked(e) {
alert(this.id);
}
document.getElementById('myButton').addEvent('onclick', clicked);
<input id="myButton" type="button" value="Click me">
本文标签: cross browserget id of clicked button javascriptStack Overflow
版权声明:本文标题:cross browser - get id of clicked button javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745255591a2650078.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论