473,507 Members | 2,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding onclick for all elements in webpage

4 New Member
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");}
  3.  
  4. var list = document.getElementsByTagName('*');
  5. len = list.length;
  6.  
  7. for(i = 0;i < len;i++)
  8. {
  9. obj=list[i].tagName;
  10. obj.onclick=getIt;
  11. }
Nov 29 '08 #1
7 1997
acoder
16,027 Recognized Expert Moderator MVP
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
kk11
4 New Member
Thanks very much..got 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
acoder
16,027 Recognized Expert Moderator MVP
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
kk11
4 New Member
I don't know how to do any of those
Dec 1 '08 #5
acoder
16,027 Recognized Expert Moderator MVP
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
kk11
4 New Member
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
acoder
16,027 Recognized Expert Moderator MVP
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

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

Similar topics

15
12349
by: crjunk | last post by:
I have 4 TextBoxes on my form that I'm trying to add together to get a grand total. Here is the code I'm using: <SCRIPT LANGUAGE="JavaScript"> <!-- Beginning of JavaScript - function...
11
10174
by: charlie_M | last post by:
I have a number of applications where I want to have many onclick='submit()' attached to different 'elements' on a single form ..... which sends the form to a CGI "script" which does all the...
3
6859
by: Newcomsas | last post by:
Hello, I'm trying to solve a problem with JS textbox array without success. I have two buttons in my page: PLUS and MINUS; at every click on PLUS a new textbox named 'dear' is generated. So, if...
4
2033
by: domas | last post by:
Hi, I can't get my semi-dynamic form to work. Maybe you could help. I've got a simple form with a select box. And a button that, when clicked, creates another copy if the select box and places...
5
1861
by: yawnmoth | last post by:
I'm having some difficulty with adding elements to a webpage via the DOM. The following works: main.htm: <script> js = document.createElement('script'); js.src='test.js';...
2
2171
by: Ken Fine | last post by:
In code, I'm adding javascript attributes to form elements on an ASP.NET page: body.Attributes.Add("onClick", "highlight(event);"); body.Attributes.Add("onKeyUp", "highlight(event);");...
11
11164
by: Daz | last post by:
Hello everyone. I am sure the answer to my question is simple, but I can't seem to dynamically add an onClick event to my script. I have a table which is generated dynamically, I am just...
1
3183
by: Jacqui | last post by:
Hi, I am trying to write a webpage that has a form on it, which gets submitted when a button is pressed, but also has an onclick event which does some DOM manipulation to display a waiting page....
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7111
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.