Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to determine if object is null/empty

Status
Not open for further replies.

AlisonMC

Technical User
Jan 13, 2006
4
AU
I have some JavaScript which selects items from a listbox when a button is clicked. However, there could be times when there are not items at all in the listbox. How do I determine this so that the full JavaScript does not run if there are no items in the listbox?

Currently I have the following code shown below, but I don't want to assign anything to intCount if the listbox is empty. How do I go about that? - Thanks.

function SelectedItems(){
var intCount = window.document.myDataEntryForm.ListboxMinor.options.length;
var intCountC = window.document.myDataEntryForm.ListboxMajor.options.length;

if (intCount > 0){
for (i = 0; i < intCount; i++){
window.document.myDataEntryForm.ListboxMinor.options(i).selected = true;
}
}

if (intCountC > 0){
for (i = 0; i < intCountC; i++){
window.document.myDataEntryForm.ListboxMajor.options(i).selected = true;
}
}
}
 
I'm rather new to programming with JS, so I may be wrong - but try this:

function SelectedItems(){
if(window.document.myDataEntryForm.ListboxMinor.options.length == null)
{
alert("No Items Exist");
{
else
{
var intCount = window.document.myDataEntryForm.ListboxMinor.options.length;
var intCountC = window.document.myDataEntryForm.ListboxMajor.options.length;

if (intCount > 0){
for (i = 0; i < intCount; i++){
window.document.myDataEntryForm.ListboxMinor.options(i).selected = true;
}
}

if (intCountC > 0){
for (i = 0; i < intCountC; i++){
window.document.myDataEntryForm.ListboxMajor.options(i).selected = true;
}
}
}
}
 
You don't need those if statements at all. Additionally, if there are no options in the select box, then .length will return 0.

have a look:

Code:
function SelectedItems(){

    var s1 = document.forms['myDataEntryForm'].elements['ListboxMinor'];
    var s2 = document.forms['myDataEntryForm'].elements['ListboxMajor'];

    // if there are no options, length is 0.
    // i = 0, and 0 is not less than 0, so this loop will never run
    for (i = 0; i < s1.options.length; i++)
        s1.options[i].selected = true;
 
    // same thing here...
    for (i = 0; i < s2.options.length; i++)
        s2.options[i].selected = true;
}



*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Thinking about it a bit more, I think the issue is that in some cases the listbox referred to is present on the page and in others it is not.

How can JavaScript code detect the presence or absence of that listbox and either run the code or bypass it if the listbox is not on the page?

Thanks!
 
Worked it out :) like this:
Code:
function SelectedItems()
{
var lstMinor=document.getElementsByName("ListboxMinor")
var lstMajor=document.getElementsByName("ListboxMajor")
if ((lstMajor.length != 0)&&(lstMinor.length !=0)){
	/* do nothing */
}
alert(lstMajor.length + " elements!")
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top