By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,451 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,451 IT Pros & Developers. It's quick & easy.

Adding onclick for all elements in webpage

P: 4
Hi all,
I've been trying to add onclick event to all elements in the webpage. But somehow it doesn't work. Any help would be greatly appreciated.

Here's my code:

Expand|Select|Wrap|Line Numbers
  1. function getIt()
  2. {alert("Hello");}
  4. var list = document.getElementsByTagName('*');
  5. len = list.length;
  7. for(i = 0;i < len;i++)
  8. {
  9. obj=list[i].tagName;
  10. obj.onclick=getIt;
  11. }
Nov 29 '08 #1
Share this Question
Share on Google+
7 Replies

Expert Mod 15k+
P: 16,027
Hi, welcome to Bytes!

The problem with your code is that you're setting onclick to the tag name instead of the actual object. So set list[i] instead of list[i].tagName.
Nov 29 '08 #2

P: 4
Thanks very it fixed

I also had another question...if I've a lot of content inside a div..
like "When are you coming home?"

and I need to get only "When are you" when I move my mouse over the first part of the div and when I move it over to the next part of the div, I want to get the rest of the text, ie, "coming home?"

I get the whole stuff using innerHTML property but I don't know how to get only the part of text above which my mouse pointer is on.
Nov 30 '08 #3

Expert Mod 15k+
P: 16,027
Either split the div into two divs, or get the mouse position (see mouse position) and display the required text depending on which part of the div the mouse is.
Nov 30 '08 #4

P: 4
I don't know how to do any of those
Dec 1 '08 #5

Expert Mod 15k+
P: 16,027
Which one would you like to do? The first one is easy, but manual and a fix. The second is more advanced, but you won't need to change it if the content changes.
Dec 1 '08 #6

P: 4
I want to create a magnifyier that magnifies text in any webpage on mouse click, so I would prefer the 2nd one as it works with any content
Dec 1 '08 #7

Expert Mod 15k+
P: 16,027
How much would it need to magnify and how would you decide how much? The mouse position will tell you which part of the div the mouse is in, but not exactly what text is underneath it because that depends on how the browser renders the text at what height, width, spacing, etc.
Dec 2 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.