•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 455,972 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,808 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 5613 | Replies: 5
![]() |
•
•
Join Date: Oct 2007
Posts: 34
Reputation:
Rep Power: 2
Solved Threads: 0
hi, i'm wondering why is it javascript doesn't seem to accept null value as a real null? In order to know if its a null field later on, it's as if i have to check it against a string 'null'.
for example,
i have a txtName hidden field on my html.
I use javascript to document.myForm.txtName.value = null;
later on, when i try to detect it just doesn't work :
if(document.myForm.txtName.value == null){
alert('value is null');
}else if(document.myForm.txtName.value == 'null'){
alert('value is a string null'); //only this works!
}
for example,
i have a txtName hidden field on my html.
I use javascript to document.myForm.txtName.value = null;
later on, when i try to detect it just doesn't work :
if(document.myForm.txtName.value == null){
alert('value is null');
}else if(document.myForm.txtName.value == 'null'){
alert('value is a string null'); //only this works!
}
•
•
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation:
Rep Power: 3
Solved Threads: 71
•
•
Join Date: Oct 2007
Posts: 34
Reputation:
Rep Power: 2
Solved Threads: 0
•
•
•
•
use a variable.
var test = document.myForm.txtName.value;
test = null;
if(test == null){
alert('value is null');
}else if(test == 'null'){
alert('value is a string null'); //only this works!
}
i think this should work, but i didn't test it.
nope, don't think it'll work. I actually rewrote the sample when i started the thread, i'm actually assigning it to a variable already.
this should be something common, yet no one's talking about it??
As long an HTML input element exists, it's value can never be null. It can be a blank string (string of length zero), but not null. And if that element doesn't exist, it gives you an 'undefined' instead but never null.
So, to check whether an element exists or not:
Also keep in mind that the DOM function getElementById() returns a 'null' when an element is not found in the DOM tree.
<html> <body id="bdy"> <form action="#" name="frm"> <input id="txt" name="txt" /> </form> <script> alert(document.forms['frm'].elements['txt']); alert(document.forms['frm'].elements['ok']); </script> </body> </html>
So, to check whether an element exists or not:
if(typeof(document.forms[0].elements['elementName']) === 'undefined')
{
// an element by the name of 'elementName' doesn't exist
}Also keep in mind that the DOM function getElementById() returns a 'null' when an element is not found in the DOM tree.
var e = document.getElementById('elementId');
if(e == null)
{
// an element by the id of 'elementId' doesn't exist
} Last edited by ~s.o.s~ : Nov 24th, 2007 at 1:22 am.
I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Every primitive value in Ecmascript is one of the following types: Null, Undefined, Number, Boolean and String. Null is a type in Ecmascript having only one value, null. The details are implementation specific. Calling it a pointer would be wrong.
I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Reading out to a file/Null String Troubles (C++)
- how to compare a string to null... (C++)
- breaking up a String... (a bit more advance) (Java)
- Please help me in string operation problem (Java)
- what function can delete a space in a string? (C)
- repost: leak using c++ string (C++)
- leak using c++ string (C++)
- change statement (JSP)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Move Javascript Variable to PHP
- Next Thread: Loading External Website pages using ajax



Linear Mode