JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 11th, 2006, 04:52 PM
Eric M Eric M is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 75 Eric M User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 14 m 24 sec
Reputation Power: 5
How to set onclick event of a link in script?

Why cant i set the onclick event of a link in javascript?

No errors but does not work:

if (document.all[i].tagName == "A"){
document.all[i].onclick = "myfunction();";
}

Object does not support this property or method:
document.all[i].addEventListener("click", "myfunction();", true);

But i can set the .href with no problem and it works.

So how can i set the onclick event in script?
Please dont reply to say to use something like <a onclick=""> i want to do it in code not in html!

Reply With Quote
  #2  
Old October 11th, 2006, 06:17 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 22nd Plane (15500 - 15999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 15,942 Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 3 Weeks 5 Days 11 h 58 m 27 sec
Reputation Power: 1522
Don't use document.all. It's obsolete. Use DOM1+ methods, like document.getElementById() and document.getElementsByTagName(), instead.

Your other problem is that functions aren't strings.

Code:
someElement.onclick = myfunction;

OR
Code:
someElement.onclick = function() {
  myfunction();
}
__________________
Spreading knowledge, one newbie at a time.

Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions

Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

I'm looking for new clients.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > How to set onclick event of a link in script?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway