faqts : Computers : Programming : Languages : JavaScript : Forms

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

12 of 35 people (34%) answered Yes
Recently 2 of 10 people (20%) answered Yes

Entry

is it possible to update a form field by clicking on an element (href, word, span or whatever) cross browser ?

May 29th, 2000 13:19
Mike Hall, David Gurney, http://developer.netscape.com/docs/manuals/htmlguid/index.htm


While IE support events for almost every HTML tag, Netscape does not. To 
make your code compatible, you must limit yourself to using those 
elements for which Netscape does support event capturing. See the 
Netscape HTML guide (link above) to determine what tags support the 
ONCLICK event.
Here's one example that uses hypertext links to check a given radio 
button option. It will work on both NS and IE.
<html>
<head>
<title></title>
<script language="JavaScript">
function setColor(color) {
  if (color == "red")
    document.forms['myForm'].elements['color'][0].checked = true;
  if (color == "green")
    document.forms['myForm'].elements['color'][1].checked = true;
  if (color == "blue")
    document.forms['myForm'].elements['color'][2].checked = true;
  return false;
}
</script>
</head>
<body>
<form name="myForm">
  <input name="color" type="radio" value="red">
  <a href="#" onclick="return setColor('red');">Red</a><br>
  <input name="color" type="radio" value="green">
  <a href="#" onclick="return setColor('green');">Green</a><br>
  <input name="color" type="radio" value="blue">
  <a href="#" onclick="return setColor('blue');">Blue</a>
  <p>
  <input type="submit"> <input type="reset">
</form>
</body>
</html>