By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,689 Members | 1,901 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,689 IT Pros & Developers. It's quick & easy.

javascript: variable has no properties

P: 1
im trying to iterate thru form elements and pull out the ones with "note" in their name. getting "elementName has no properties" with the following code, and i don't understand why? any help is greatly appreciated. thx!

Expand|Select|Wrap|Line Numbers
  1. Code:
  2.  
  3. function saveNotes(){ 
  4. var elementName=''; 
  5. var isNote=''; 
  6. for(i=0; i<document.formNotes.elements.length; i++){ elementName=document.formNotes.elements[i].name; isNote=elementName.indexOf('note'); 
  7. if (isNote!=-1) {
  8.  alert('its a note!'); 
  9. }
  10. }
  11. }
changing "elementName=document.formNotes.elements[i].name;" to "alert(document.formNotes.elements[i].name);" gives me the name. why won't it transfer the name of the element to the variable?

thanks!
Jun 21 '06 #1
Share this Question
Share on Google+
2 Replies


acoder
Expert Mod 15k+
P: 16,027
Without the HTML it'd be difficult to say, but the code would work with, for example:
[html]<form name="formNotes">
<input type="text" name="notes">
<input type="button" onclick="saveNotes()" value="Save">
</form>[/html]
May 1 '08 #2

100+
P: 428
Change this line-
elementName=document.formNotes.elements[i].name;
to this:
elementName=document.formNotes.elements[i].name || '';

Chances are your form has a fieldset or some other member that is counted as a form element but returns undefined for the name attribute.

Give your undefined's the empty string, which will return -1 for any indexOf call.
May 1 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.