Javascript - checking undefined

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
LVL 1
nachtmskAsked:
Who is Participating?
 
Chris StanyonConnect With a Mentor Commented:
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
 
Lee SavidgeCommented:
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
 
nachtmskAuthor Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Lee SavidgeCommented:
I see, yes that is clearer.

Try this.

var modifiedby = "NA";

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

Open in new window

0
 
nachtmskAuthor Commented:
Thank you Lee! I will try that.
0
 
nachtmskAuthor Commented:
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
 
Sar1973Commented:
Try also to change the name of the variable var modifiedby since it is the same of the field you are checking.
0
 
DualCoolCommented:
**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
All Courses

From novice to tech pro — start learning today.