Thursday, October 5, 2023
 Popular · Latest · Hot · Upcoming
192
rated 0 times [  195] [ 3]  / answers: 1 / hits: 18146  / 15 Years ago, wed, july 22, 2009, 12:00:00

I am building a small app which captures mouse clicks. I wrote the prototype in jQuery but, since it is a small app focusing on speed, embedding jQuery to use just one function would be an overkill.


I tried to adapt this example from JavaScriptKit:


document.getElementById("alphanumeric").onkeypress=function(e){  
//blah..blah..blah..
}

but it didn't work when I tried this:


document.getElementsByTagName("x").onclick

What am I doing wrong?


More From » jquery

 Answers
63

Say you have a list of p tags you would like to capture the click for the <p> tag:


var p = document.getElementsByTagName("p"); 
for (var i = 0; i < p.length; i++) {
p[i].onclick = function() {
alert("p is clicked and the id is " + this.id);
}
}

Check out an example here for more clarity:
http://jsbin.com/onaci/


[#99081] Friday, July 17, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
krystadesiraeo

Total Points: 493
Total Questions: 93
Total Answers: 100

Location: San Marino
Member since Thu, Jun 30, 2022
1 Year ago
;