Solved

Javascript - checking undefined

Posted on 2014-02-04
8
245 Views
Last Modified: 2014-02-15
Hi,
I've been reading about this but still can't figure out how to check what I need to check.

I have a form that is being submitted via Javascript.

The form is dynamic, meaning, sometimes it has a field in it called "modifiedby" and sometimes it doesn't (it depends on what action is being performed on the form)
I need to get the value that is input into the field "modifiedby" if the field is present and otherwise ignore (or insert N/A) for the value of that field.

Here is the code I've been trying, but it keeps using NA as it's value

if(modifiedby == undefined){
modifiedby = "NA";
}else{
var modifiedby = document.getElementById('modifiedby').value;
}

I've also tried
if(typedef modifiedby == undefined){
modifiedby = "NA";
}else{
var modifiedby = document.getElementById('modifiedby').value;
}


If I leave the if statement off and just use:
var modifiedby = document.getElementById('modifiedby').value;
Then I get an error message saying the field is null.

What's the proper way to do this please?

Thanks,
Nacht
0
Comment
Question by:nachtmsk
[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
8 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39832561
Firstly:

undefined, null and an empty string are different things in Javascript.

Your issue appears to be one of scope.

if(modifiedby == undefined){
modifiedby = "NA";
}else{
var modifiedby = document.getElementById('modifiedby').value;
}

Open in new window


This will ALWAYS return "NA" because you have compared modifiedby to undefined before you have declared it. Javascript will create the variable for you when you first use it, so in your case it is undefined.

The reason you get null on its own if you use your other option is that the value form the form is blank. When a variable is null in Javascript it MUST have been set to null to be that way. It's a handy way of knowing that something must have set a value at some point.

I'd need to know what you're trying to do in the bigger scheme to suggest a fix, but your code above will not do what you want... unless you always want "NA".
0
 

Author Comment

by:nachtmsk
ID: 39832603
HI Lee,
Yeah, I am getting NA all the time, your correct.

I thought I included the bigger picture in my post but here is some more info.

I have a form that in same cases has the field "Modifyed By" present and in other cases does not.

I am submitting the form via Javascript.

In the case of  the form having the field "modifiedby", I need to get that value.
In the case of the form not having the field "modifiedby", I need javascript to not throw an error. Setting the modifiedby var to "NA" is fine in this case.
I pass all of this information into a database.
Does that help?
Thanks!
Nacht
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39832625
I see, yes that is clearer.

Try this.

var modifiedby = "NA";

if(document.modifiedby)
{
  modifiedby = document.getElementById('modifiedby').value;
}

Open in new window

0
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!

 

Author Comment

by:nachtmsk
ID: 39832638
Thank you Lee! I will try that.
0
 

Author Comment

by:nachtmsk
ID: 39833319
Hi Lee,

The code you posted  isn't getting  into the if.
This is being done though some homegrown Ajax code I created (which is working fine).
Is there another check that would be more appropriate in this situation to see if modifiedby exists?

Thanks,
Nacht
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39835156
Try also to change the name of the variable var modifiedby since it is the same of the field you are checking.
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39844750
This is what you need:

element =  document.getElementById('modifiedby');
modifiedby = (typeof(element) != 'undefined' && element != null) ? element.value : 'NA'; 
alert(modifiedby);

Open in new window

0
 
LVL 2

Expert Comment

by:DualCool
ID: 39854447
**you need to wrap your code in a conditional that checks to see if the element exists
also instead of undefined you can use string

if(document.getElementById('modifiedby')){     << this part checks to see if the element exists
     var modifiedby = document.getElementById('modifiedby').value;
     if (typeof(modifiedby) == 'string') {
        //do stuff
      }
}
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

690 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