Solved

Javascript - checking undefined

Posted on 2014-02-04
8
238 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
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
 

Author Comment

by:nachtmsk
ID: 39832638
Thank you Lee! I will try that.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 42

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now