Connecting Tech Pros Worldwide Help | Site Map

javscript: variable has no properties

Newbie
 
Join Date: Jun 2006
Posts: 1
#1: Jun 21 '06
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!
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: May 1 '08

re: javscript: variable has no properties


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]
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#3: May 1 '08

re: javscript: variable has no properties


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.
Reply


Similar JavaScript / Ajax / DHTML bytes