|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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! |
|
#2
|
||||
|
||||
|
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. Check out my blog. |
![]() |
| Viewing: Dev Shed Forums > Web Design > JavaScript Development > How to set onclick event of a link in script? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|