Jump to content

onClick Help


irrumate

Recommended Posts

Hello,I was wondering if there was a way to take a simple onClick command such as:<HTML><HEAD><TITLE>OnClick alert message</TITLE><script LANGUAGE="JavaScript">function pushbutton() { alert("Here is where you write what you want it to say");}</SCRIPT></HEAD><BODY><CENTER><A HREF="" onclick="pushbutton();">Click Here!</A></CENTER></BODY></HTML>and be able to make it work for all the links on the same page so I don't have to keep using the "a hef="" onclick="pushbutton();"" command. I am going to have a bunch of links on a page and want the same alert box message to appear when any of the links are clicked. Please leave your input. Thanks

Link to comment
Share on other sites

You can attach the event onload to all links, as such:

window.onload = function() {	var links = document.getElementsByTagName("a");	foreach (i in links) {		links[i].onclick = pushbutton; //note: no ()   }}

By the way, it is best to put for the href attribute "java script:void(0)" so that the browser doesn't do anything besides run the JS.

Link to comment
Share on other sites

By the way, it is best to put for the href attribute "java script:void(0)" so that the browser doesn't do anything besides run the JS.
I don't understand why people still use void... Wouldn't javascript: return false; do the same thing?
Link to comment
Share on other sites

You can attach the event onload to all links, as such:
window.onload = function() {	var links = document.getElementsByTagName("a");	foreach (i in links) {		links[i].onclick = pushbutton; //note: no ()   }}

By the way, it is best to put for the href attribute "java script:void(0)" so that the browser doesn't do anything besides run the JS.

How would I go about putting this into my script? (i.e.- what would the full code look like?)
Link to comment
Share on other sites

See the first item in my signature for that href.

<HTML>	<HEAD>		<TITLE>OnClick alert message</TITLE>		<script LANGUAGE="JavaScript">			window.onload = function() {				var links = document.getElementsByTagName("a");				foreach (i in links) {					links[i].onclick = pushbutton; //note: no ()			   }			}						function pushbutton() {				alert("Here is where you write what you want it to say");			}		</SCRIPT>	</HEAD>	<BODY>		<CENTER>			<A HREF="java script:void(0)" onclick="pushbutton();">Click Here!</A>		</CENTER>	</BODY></HTML>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...