Forum Moderators: open

Message Too Old, No Replies

Why doesn't document.GetElementByID("MyName").submit(); work?

         

pkirkaas

8:38 am on Oct 28, 2008 (gmt 0)

10+ Year Member



Hi;

I've seen lots of discussion & disagreement on this issue, so any good explanation would be appreciated.

Some people seem to think that "document.GetElementByID("MyName").submit(); should and does work. I and others have experienced that it should & doesn't work. I'll give you a little file that I tested with IE 6, FF 3, Opera & Chrome. Only the form button and document.forms['MyName'].submit(); worked. Take a look & let me know if I've done something wrong, or if there is a good reason that shouldn't work.

Thanks,

Chris

----- Code -----
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link type="text/css" rel="stylesheet" href="MyStyleSheet.css"/>
<title>TestJS</title>
</head>

<script language = JavaScript>
document.write("Hello World")
</script>
<body>

<FORM id="myform" name="myform" ACTION="tst.php">

<INPUT TYPE="SUBMIT" VALUE="Click for a Message Box" onclick="alert ('Button Pressed');">

<div onclick="javascript:alert('Hello!');document.myform.submit( );"> Click Me! </div>
<div onclick="javascript:alert('Goodby!');document.GetElementByI D('myform').submit();"> <br/><br/><b>No, Me!</b><br/><br/> </div>
<div onclick="javascript:alert('It is sunny today.');document.forms['myform'].submit();"> <br/><br/><b>I say, Me!</b><br/><br/> </div>

</FORM>

</body>
</html>

pkirkaas

9:38 am on Oct 28, 2008 (gmt 0)

10+ Year Member



Okay, got it, thanks. Moving from a case insensitive environment to a case sensitive environment.

rocknbil

3:04 pm on Oct 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll add that some browsers don't respond to onClick for divs, tables, cells, etc., and this causes you to look in the wrong places for the malfunction. You may get more consistent results if you use the natural object, the anchor, when implementing onClick.

<a href="#" onclick="alert('Hello!');document.getElementById('myform').submit(); return false;">Click Me!</a>