View Full Version : href submit function problem

12-01-2008, 03:10 PM
I have a javascript function as follows in my page which
<script language="javascript">
function password_check()
if(document.getElementById('new_pass1').value==document.getElementById('new_pass2').value) { document.getElementById('log1').submit();
} else{//some another activity. } }

This script is getting used by a button in HTML body,detailed below:
<form name="log" method="POST" action="<%=user_ver%>" id="log1">

<!--<%=user_ver%> is the jsp code for the address of the servlet,which doesnot have any kind of problem -->

<input name="new_pass1" type="password" id="new_pass1" size="50%" align="middle"><br>

<input name="new_pass2" type="password" id="new_pass2"
size="50%" align="middle" ><br>

<input name="Submit" onclick="password_check()" size="50" type="button" id="Submit" value="SubmiT" align="middle">

My problem is that when I hit SubmiT button it fires an error saying that document.getElementById('log1') is null. If I chnage that piece of code in function with document.log.submit(); then it says that document.log is not defined. Could you help me in this and also to find what's going wrong in this?

12-01-2008, 09:18 PM
The above mentioned errors have received in firefox. In Internet explorer, getting the error-Object does not support this property or method.

Please help.

12-01-2008, 09:57 PM
I copied your code, when I ran it I saw no errors in firefox's error console, I did take out the "//some another activity." part though.

Any more code that you've omitted and might be causing the problem?

12-02-2008, 03:48 AM
I have refined the code you've provided a bit. It seems that you are using the "ID" property on every element, which I thought unnecessary and can be avoided.

1. Also I have noticed that in your code you've used the "size" property associated with the "password" textbox and button element incorrectly. The "size" is a property that usually used with text box elements to denote how many character can be entered into them. It doesn't represent the "width" of the element. If you want to adjust the "width" of the input elements you can use CSS like the following manner.

<input name="new_pass1" type="password" style="width: 150px;">

2. I have avoided the usage of inline event handling in my code in which I've associated the event handler through JS code

3. I have avoided the usage of deprecated "align" property from input elements. If you want to align the elements try to use CSS.

Here is the modified JS and HTML markup

<script type="text/javascript">
function password_check(){
var form = document.forms['log']; //Form element
if(form && form.elements['new_pass1'].value === form.elements['new_pass2'].value) {
} else{
alert('Either the form object is null or the value checking is false);

window.onload = function(){
var form = document.forms['log'];
form.elements['Submit'].onclick = password_check;

<form name="log" method="POST" action="<%=user_ver%>">
<input name="new_pass1" type="password" size="50"><br>
<input name="new_pass2" type="password" size="50"><br>
<input name="Submit" type="button" value="SubmiT">

Hope this helps.

12-02-2008, 04:56 PM
Thanks for the info, One more thing which I want to know about what if I call this above discussed page in another page and then submit the page. Now I figured out that it is not the code problem actually since I am calling this page within another page and then submiting the page that is why it is producing this error.Something like:

//here calling the above page :I'm using JSP:include tag which is like another method of other languages ,for that

In this scenario, how would we make submit button to submit the page with all hidden values and other component's entries of the page?