I would like to have a text with an onclick attribute that executes something when clicked. The following solution works in Internet Explorer and Netscape6 but does not work in Netscape4:
<div onclick="action()">
Click here
</div>
Does anyone have a solution for achieving this in Netscape4?
You'd have to surround the Div with a link, i'd say. NS4 only supports certain events on certain elements. I rely on a table in the Guide (see my sig) to see which elements support which events in which browsers.
Netscape 4 version browsers do not support the onClick in a div tag. You could place some javascript withing the div to check for NS4 and write a link. Something like this:
<DIV>
<SCRIPT LANGUAGE="JavaScript">
if(document.layers)
{
document.write("<A HREF='page.html'>Click Here</A>")
}
</SCRIPT>
</DIV>
The above would only write that out if the browser was NS4. Also make sure that you use single quotes within double quotes.
My first example was not very good. Try this one (hopefully it is better).
<html>
<head>
<title>Onclick in a div</title>
<SCRIPT LANGUAGE="JavaScript">
function doalert() {
alert("This worked!")
}
</SCRIPT>
</head>
<body>
<DIV ID="mydiv" onClick="doalert()">
<SCRIPT LANGUAGE="JavaScript">
if(document.layers)
{
document.write("<A HREF='javascript:doalert()'>Click Here</A>")
}
else
{
document.write("Click Here")
}
</SCRIPT>
</DIV>
</body>
</html>
Thank you for your suggestions.
In fact, what I want to do is a table whose rows can be selected, in the sense that they are highlighted when clicked and the clicked row number is memorized. I tried to put the whole row in an anchor, but this does not work, it only works with individual table data (td elements). Isn't there a simpler solution?