reset javascript not working for dynamic text

Hi ,

 I am using the following java script to generate a dependent text-box when "Other" is selected out of the dropdown for market type.

My issue is that, when I use reset link to reset the values, it's working for all the fileds except for the
"other market" text field that shows up when the "other" is selected from the market type dropdown.

I cannot see any reason why it won't work for the "other market" text field, when the same code  works for  text field fields like "email" and "phone number".

Thanks.

  TEST.html
Gopal2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaitu chaituCommented:
check this.

function otherMarketDisplay(doc,elem) {
txt = doc.options[doc.selectedIndex].value;
document.getElementById(elem).style.display = 'none';
if ( txt.match(elem) ) {
document.getElementById(elem).style.display = 'block';
} 
}

function clearForm() {
   
		document.getElementsByName('Phone Number')[0].value = '';
		alert('before getElementsByName(Email)[0].value = ;');
		document.getElementsByName('Email')[0].value = 'Required';
		alert('AFTER getElementsByName(Email)[0].value = ;');	
		alert('before getElementsByName(Primary Market)[0].value = ;');	 
		document.getElementsByName('Primary Market')[0].selectedIndex = 'Primary Market'; 
		alert('AFTER getElementsByName(Primary Market)[0].value =Primary Market ;');		
		//document.getElementsByName('Company Type')[0].selectedIndex = 'Company Type'; 
		alert('before getElementsByName("Other Market")[0].value = ;');
		document.getElementsByName("Other Market")[0].value = '';
		alert('AFTER getElementsByName("Other Market")[0].value = ;');
		

	}

</script> 
</head>
<body>
<table>

<tr>
<tr>
<td><div class="formLabel">Phone Number </td>
<td><input name="Phone Number" value="<%=phoneNumber%>" type="text" onfocus ="clearText(this)" style="width:294px;"/></td>
</tr>

<tr>
<td><div class="formLabel">E-mail Address </td>
<td><input id="validate" name="Email" value="<%=email%>" type="text" onfocus ="clearText(this)" style="width:294px;"/></td>
</tr>

<td><div class="formLabel" style="text-align:left">market type?</td>

<td><select id="validate" name="Primary Market"  style="width:300px;" onchange="otherMarketDisplay(this,'otherMarket');">
<option value="<%=primaryMarket %>"><%=selectedPrimaryMarket %></option>
<option value="ATech">ATech</option>
	<option value="Btech">Btech</option>
	<option value="DTech">DTech</option>
	<option value="GTech">GTech</option>
	<option value="LabTech">LabTech</option>
	<option value="MTech">MTech</option>
	<option value="otherMarket">Other</option>
</select>
</td>
</tr>

<tr>
<tbody id="otherMarket" style="display: none;">
<tr>
<td><div class="formLabel" style="text-align:left">Other Market</div></td>
<td><input type="text" name="Other Market" value="<%=otherMarket%>"  onfocus ="clearText(this)"/></td>
</tr>
</tbody>

<tr>
<td width=80>		
<td align="left"><a href="javascript:clearForm()">Reset Form</a></td>
</tr>
</table>

</body>
</html>

Open in new window

0
chaitu chaituCommented:
i didnt find 'Company Type' drop down so i commented it.apart from this when you are givning names or id's to text boxes and dropdowns gdont use spaces in between the names;

use 'Company_Type'  Other_Market
0
chaitu chaituCommented:
check this on how to reset the form.


http://www.javascript-coder.com/javascript-form/javascript-reset-form.phtml

function clearForm(oForm) {
var frm_elements = document.forms[0].elements;
for (i = 0; i < frm_elements.length; i++)  
{  

    field_type = frm_elements[i].type.toLowerCase();  
    switch (field_type)  
    {  

    case "text":  
    case "password":  
    case "textarea":  
    case "hidden":  
        frm_elements[i].value = "";  
        break;  
    case "radio":  
    case "checkbox":  
        if (frm_elements[i].checked)  
        {  
            frm_elements[i].checked = false;  
        }  
        break;  
    case "select-one":  
    case "select-multi":  
        frm_elements[i].selectedIndex = -1;  
        break;  
    default:  
        break;  

    }  

} 

Open in new window

0
Gopal2Author Commented:
Hi all,

  In fact my issue was that, I was using "Other" in many places in my code, hence the script was confused as to how to act on the code. So I had to make the labels and variables slightly different from
each other, then it worked.

  Thank you all for your help and happy new year.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gopal2Author Commented:
It was an issue of confusion because of the way the variables / labels were set up with the same name as per the requirements.That had to be worked on carefully to over come the confusion for the java script.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.