2

I am trying to pass form name dynamically. But this script is showing an error.

"document.formName is undefined". How to solve this. Thanks in advance.

<script language="javascript">

function showAll(form,fieldName) {
    var formName = form.name;
    alert(formName);
    document.formName.search_mode.value = "";   
    document.formName.fieldName.value="";
    document.formName.submit(); 
}
</script>
<form name = "dealsManagement" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="hidden" name="search_mode" value="<?php echo $_REQUEST['search_mode'];?>" >
<table border="0" cellpadding="0" cellspacing="0" align="center" width="400" style="padding:2px; border:#ccc solid 1px; margin:15px auto 15px auto;">
<tr>
<td colspan="2" class="text3" align="center" height="35" bgcolor="#cccccc">Search</td>
</tr>
<tr>
<td width="153" height="35" align="right" class="text2" style="padding-right:10px;">Search By  City:</td>
<td width="245"><input type="text" name="city" value= "<?php echo $_POST['city']; ?>" size="24" ></input></td>
</tr>
<tr>
<td height="30">&nbsp;</td>
<td><input name="button" type="button" class="btn" onClick="javascript:dealSearchCity()" value="Search" />
<input name="btnShowAll" type="button" class="btn"value="Show All" onClick="javascript:showAll(this.form,'city');" /></td>
</tr>
</table>
</form>
1

2 Answers 2

3

What you are doing is not necessary. You can use form directly:

function showAll(form,fieldName) {
    var formName = form.name;
    alert(formName);
    form.elements.search_mode.value = ""; 
    form.elements[fieldName].value="";     
    form.submit(); 
}

two sidenotes:

The language attribute is deprecated, better use

<script type="text/javascript">

and you don't need to prefix event handlers with javascript:.

Sign up to request clarification or add additional context in comments.

9 Comments

i am trying to use that one js method for multiple forms. I hope now it makes sense
@Fero it does, but there is still absolutely no need to do what you are doing. If you pass the right form, it will work with multiple forms no problem.
@Pekka "If you pass the right form"-> As i am a newbie i can't understand this statement. Could you please explain.
@Fero you are passing the form object this.form as a parameter to showAll(). That already makes the function compatible with multiple forms. There is no need to take the formName detour because the form in question is already there. See what I mean?
@Pekka: If i use the form then i get [object HTMLFormElement] in alert. If i use the form instead of formName i got the same error. will you please help me out of this issue.
|
0

If you want to use a string rather than a literal property name, you have to use square bracket notation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.