473,769 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Undefined and null in Javascript

Hi,

I am working on a web application which among other things uses DHTML, Java
and Javascript.
It populates web page based on the contents of the database (resultset),
and next to each row there is a checkbox (v1) allowing to select that row
for changes (e.g. delete, update, etc.)
So we are creating an array of checkbox, correct ?
Of course I have to check whether any of these checkboxes exist and if any
of them got selected (checked)
using Javascript before allowing any DB operation in Java.

Now it becomes strange.
I have noticed that if there is 1 row [i.e. 1 checkbox v1], that function
always shows warning and returns false:
function isAnySelected(c heckbox) //check if at least one checkbox is
selected
{
for(i=0; i<checkbox.leng th; i++) {
if(checkbox[i].checked) {
return true;
}
alert("You must choose at least one record.");
return false;
}
Why ? I call it as: onClick="if(! isAnySelected(v 1)) return false;"
I tried to fix that error, and noticed that in the case of a single row
checkbox.length is "undefined" .
So I modified that function into:

function isAnySelected(c heckbox) //OK: check if at least one checkbox is
selected
{
if(checkbox.len gth = "undefined" ) { // with single '=' !!!
if(checkbox.che cked) {
return true;
}
} else {
for(i=0; i<checkbox.leng th; i++) {
if(checkbox[i].checked) {
return true;
}
}
}
alert("You must choose at least one record.");
return false;
}

What I can't understand here is why that function works only when there is a
single '=' here:
if(checkbox.len gth = "undefined" )... ???
Checking for null produces Javascript errors or does nothing.

And how can I make it work if there are 0 rows [and thus 0 instances of
checkbox v1 ?]

Thank you in advance,
Oleg.
Oct 29 '06
13 39443
VK
1) JavaScript runtime error: "v1" is undefined is coming from the
function call:
if (! isAnySelected(v 1)) return false;
not from inside the function.
Are you sure that this check can fix that ?
Yes, I am: unless v1 was not declared anywhere in your program before
if (! isAnySelected(v 1)) return false
"declared" would mean that somewhere in your program you have
var v1;
or
var v1 = something;
Also I see a strong "Java school" in this line (the famous
"truthulize r" joke about you, guys :-)
return isAnySelected(v 1)
does the same as
if (! isAnySelected(v 1)) return false
else return true
as you may guess

If v1 declared but has undefined value then inside the function you
trying to access the property of undefined : undefined.lengt h and it
trigs the error. So first you check if v1 is not undefined and only
then you try to access a property of it. You may move the check level
up if you want (at the moment of the function call):
if (typeof v1 != 'undefined') { return isAnySelected(v 1); }
2) if (typeof check == 'object') {...
Hmm, do you mean that if that collection is not empty, it's an 'object' ?
Never thought of it that way.
That depends on how are getting v1. Really: the whole HTTP page is
needed to see what and how are you doing (brought to the minimum case
if possible).

Nov 12 '06 #11
VK

VK wrote:
if (typeof v1 != 'undefined') { return isAnySelected(v 1); }
Better:

return (typeof v1 != 'undefined') ? isAnySelected(v 1) : false;
// otherwise with undefined v1 there will be no explicit
// return value which is dangerous.
// still posting the entire working page is more than
// suggested.

Nov 12 '06 #12
VK,

It would be very difficult to show the whole XSL page with all HTML &
Javascript,
it is a part of old large Cocoon/XSLT/Java application.

But the part related to v1 checkbox would be :

for each DB row (become XML nodes) - we are looping through them:
<input name="v1" type="checkbox" >
<xsl:attribut e name='value'><x sl:value-of
select='$therow/id'/></xsl:attribute>
</input>
--the value=therow/id to identify the row for operation on it
[like delete or modify or compare or display all detailed values]
So if the query returned no rows, there will be no instances of checkbox on
the page.

Hope it answers your question.

I will try your original and modified solutions tomorrow and let you know.

Thank you,
Oleg.
"VK" <sc**********@y ahoo.comwrote in message
news:11******** **************@ k70g2000cwa.goo glegroups.com.. .
>
VK wrote:
> if (typeof v1 != 'undefined') { return isAnySelected(v 1); }

Better:

return (typeof v1 != 'undefined') ? isAnySelected(v 1) : false;
// otherwise with undefined v1 there will be no explicit
// return value which is dangerous.
// still posting the entire working page is more than
// suggested.

Nov 13 '06 #13
VK

Oleg Konovalov wrote:
But the part related to v1 checkbox would be :

for each DB row (become XML nodes) - we are looping through them:
<input name="v1" type="checkbox" >
<xsl:attribut e name='value'><x sl:value-of
select='$therow/id'/></xsl:attribute>
</input>
--the value=therow/id to identify the row for operation on it
[like delete or modify or compare or display all detailed values]
So if the query returned no rows, there will be no instances of checkbox on
the page.
This way the resulting HTML output will be like
<input type="checkbox" name="v1">Recor d 1</input>
<br>
<input type="checkbox" name="v1">Recor d 2</input>
<br>
<input type="checkbox" name="v1">Recor d 3</input>
(I'm sure you are using table rows instead, but for a short idea is
enough)

This way the suggested check could be (see comments inside):

<html>
<head>
<title>Checkbox es</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">

<script type="text/javascript">

function validate(refFor m) {
var ret = false;

// ... other form validations ...

// Please note that "v1" is a string literal
// (form element name), not an object:
ret = isAnySelected(r efForm.elements["v1"]);

return ret;
}
function isAnySelected(c heck) {
var ret = false;

// if at least one matching element found:
if (typeof check != 'undefined') {

// if several matching elements founds:
if (check.length) {
// intermediary var to speed up the loop:
var len = check.length;
for (var i=0; i<len; ++i) {
// if the element has boolean field "checked"
// and this field is set to true:
if ((typeof check[i].checked == 'boolean')
&& (check[i].checked)) {
ret = true;
break;
}
}
}

// if only one matching element found
// and the element has boolean field "checked"
// and this field is set to true:
else if (typeof check.checked == 'boolean') {
ret = check.checked;
}

// Else do nothing, just balance if-else clause:
else {
/*NOP*/
}

}

return ret;
}
</script>

</head>

<body>
<form method="POST" action="" onsubmit="retur n validate(this)" >
<fieldset>
<input type="checkbox" name="v1">Recor d 1</input>
<br>
<input type="checkbox" name="v1">Recor d 2</input>
<br>
<input type="checkbox" name="v1">Recor d 3</input>
<br>
<input type="submit">
</fieldset>
</form>
</body>
</html>

Nov 14 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
46575
by: John Ramsden | last post by:
.... when the id 'junk' doesn't exist anywhere in the document, instead of returning 'object'?! I am using Javascript for a drop-down menu, slightly adapted from one by Angus Turnbull (see http://javascript.internet.com and http://gusnz.cjb.net, not that this is probably relevant but it deserves a plug ;-), on Internet Explorer v6.0.2800.1106. I need this feature of getElementById(), or an equivalent one (using sound and standard...
7
2341
by: OzThor | last post by:
in javascript, is there a way to test if an array is undefined???? eg... for (var x=0; x<10; x++){ if test_array is undefined then x++ document.write(test_array) if anyone can help, thanks... oh and if you can get the syntex right for me thanks..
13
3087
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled across "null" string. I know that I will get an "undefined" when I try to retrieve something from the DOM which doesn't exist. I have used null myself to initialize or reset variables. But in which
49
14522
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On http://www.webreference.com/programming/javascript/gr/column9/ they say: <snip> The undefined property A relatively recent addition to JavaScript is the undefined property.
17
3350
by: yb | last post by:
Hi, Looking for clarification of undefined variables vs. error in JavaScript code. e.g. <script> alert( z ); // this will be an error, i.e. an exception </script>
45
4858
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to "new Array() vs " question or to the array performance, I dared to move it to a new thread. Gecko takes undefined value strictly as per Book 4, Chapter 3, Song 9 of Books of ECMA :-)
2
1584
by: udieee | last post by:
Hi, When I run the following function in IE5.5 with SP2 (version 5.50.4807.2300.co ) I get the javascript error for 'undefined'' function checkParam(url, paramName, paramValue) { if(paramName!=undefined && paramValue!=undefined && paramName!=null && paramValue!=null && paramName.length==paramValue.length) {
11
8965
by: redog6 | last post by:
Hello I have the above javascript error on filling in mandatory fields and submitting the form below: http://www.wildwoodbushcraft.com/voucherformtest.htm?course_name=Bushcraft+Weekend+course&price_code=SWC This error only occurs in IE not firefox and I can't see anything wrong with the functioning of the form. I have been tearing my hair out on this one and would be very
4
8420
hemantbasva
by: hemantbasva | last post by:
We have designed an aspx page having five ajax tab in it. the data of first four are designed on page whereas for the fifth tab it renders a user control named MYDOMAIN. in the tab container's even onactivetabindexchanged we have called a method loadtabpanel() which is defined in javascript in same page.the problem is it sometime give the message load tab panel undefined. this error does not come regularly. it comes in usercontrol rendering . i...
0
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.