User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2007
Posts: 34
Reputation: adrive is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adrive adrive is offline Offline
Light Poster

javascript null..is it a string?

  #1  
Nov 23rd, 2007
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!
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 71
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Practically a Master Poster

Re: javascript null..is it a string?

  #2  
Nov 24th, 2007
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.
Reply With Quote  
Join Date: Oct 2007
Posts: 34
Reputation: adrive is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adrive adrive is offline Offline
Light Poster

Re: javascript null..is it a string?

  #3  
Nov 24th, 2007
Originally Posted by kkeith29 View Post
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??
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 7,009
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 368
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: javascript null..is it a string?

  #4  
Nov 24th, 2007
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.
<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.
Reply With Quote  
Join Date: Jan 2007
Posts: 2,604
Reputation: MidiMagic is on a distinguished road 
Rep Power: 7
Solved Threads: 119
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Maven

Re: javascript null..is it a string?

  #5  
Nov 25th, 2007
Null is a pointer that doesn't point to anything.
Daylight-saving time uses more gasoline
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 7,009
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 368
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: javascript null..is it a string?

  #6  
Nov 25th, 2007
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 9:12 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC