Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

javascript field undefined not picking up

Posted on 2014-01-29
5
Medium Priority
?
255 Views
Last Modified: 2014-02-02
I have some form fields that I am trying to validate.
I'm not sure that all fields are on the page.
so I tried checking first if the field is defined and then I run the rest of the function.
if(document.getElementById(fieldname)) {
		if(document.getElementById(fieldname).value == "" ) {
			 themessage = (themessage + fieldname+ '\n');
	
			}		}
	}

Open in new window

yet, I am getting the following error in my console
Uncaught TypeError: Cannot read property 'value' of undefined
0
Comment
Question by:rivkamak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39819133
Where is fieldname coming from? Is that the ID of the input?
Post your whole code.
0
 

Author Comment

by:rivkamak
ID: 39819718
function validate(fieldname, labelname, longname) {
	longname = typeof longname !== 'undefined' ? longname : fieldname;
	if(document.getElementById(fieldname)) {
		if(document.getElementById(fieldname).value == "" ) {
			 themessage = (themessage + longname+ '\n');
	
			clearLabelcss(fieldname,"error-redInput error-redSelect" );
			clearLabelcss(labelname,"error-red" );
			_gaq.push(['_trackEvent', 'form errors', 'onsubmit individual', longname,,true]); 
			ga('send', 'event', 'form errors', 'onsubmit individual', longname,{'nonInteraction': 1});
	
			
		}
	}
}

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 39819723
...and the html for inputs
0
 

Author Comment

by:rivkamak
ID: 39819730
I use this in the javascript at a later point.

Open in new window


validate('First', 'FirstLabel', 'First Name');

the html looks like this
<label id="FirstLabel" for="First"></label>

<input id="First" type="text" maxlength="50" value="" x-autocompletetype="given-name" title="first name" name="First" style="border-width: 1px; border-style: solid; -moz-border-top-colo…rs: none; -moz-border-left-colors: none; border-image: none;"></input>

Open in new window


Sometimes the field is available and sometimes it isn't.
I need to make sure the script doesn't throw an error if the field is removed.
0
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 39819753
With a field
http://jsfiddle.net/GaryC123/3Lf6P/3/

Without a field
http://jsfiddle.net/GaryC123/3Lf6P/1/

Cleaned up JS as it wasn't valid based on what you posted

 
   function validate(fieldname, labelname, longname) {
	themessage="Please enter the ";
    longname = typeof longname !== 'undefined' ? longname : fieldname;
	if(document.getElementById(fieldname)) {
        alert("Field exists")
		if(document.getElementById(fieldname).value == "" ) {
			 themessage +=longname+ '\n';
	
   clearLabelcss(fieldname,"error-redInput error-redSelect" );
   clearLabelcss(labelname,"error-red" );
   		_gaq.push(['_trackEvent', 'form errors', 'onsubmit individual', longname,,true]); 
    		ga('send', 'event', 'form errors', 'onsubmit individual', longname,{'nonInteraction': 1});
	
alert(themessage)				
		}
	}
} 

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

596 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question