Ok guys, I need some help with this one. I've been beating my head for a day now trying to get this code to work and have gotten nowhere. Let me give you an idea how this web app works:

What this does is create a ticket with a date and a list of employees. I have a text box (later defined as wDate) and 2 listboxes, one is filled with employees, and the other is empty. I have a script that moves an employee over to the empty list (and back, if needed) without any problems.

However, I need to make sure the employee that is being moved over has not been assigned a job for the day. What happens is, when a work ticket has been made for that day, that work ticket date is updated in the employees 'wdate' column in the DB. As this is built, the 'wdate' will look like so:
"02/02/2009 02/20/2009 02/10/2009 02/21/2009"

Now, when I pull the list from the DB, the option will look like so:

<option value="19" rel="02/02/2009 02/20/2009 02/10/2009 02/21/2009" id="19">John Smith[***-**-****]</option>

Now, the javascript in action:

This is checking the selected employee work dates through the rel. What this function does is

  1. Check to see if rel= exists
  2. If it does, loop through the rel= Array
  3. If the date (var wDate) is in that Array, return true

This will let me know if this employee is already on a ticket from the date enter in var wDate.

function CheckSelectedItem() {

var wDate = document.workorder_form.workorderdate.value;
len = document.workorder_form.employees.length;
i = 0;
for (i = 0; i < len; i++) {
if (document.workorder_form.employees[i].selected) {
	if (document.workorder_form.employees[i].hasAttribute("rel")) {
        dt = document.workorder_form.employees[i].id //Get Selected ID
		var el = document.getElementById(dt).getAttribute("rel"); // placing the dates under rel=
		var elArray = el.split(" "); //split the dates into an "Array"
		inArr = false;
		for (a = 0; a < elArray.length; a++)
			if (wDate == elArray[a])
					inArr = true;
				return inArr;

And this is the call to move the employee over:

function move(fbox, tbox) {
var csi = CheckSelectedItem();
var arrFbox = new Array();
var arrTbox = new Array();
var arrLookup = new Array();
var i;
for (i = 0; i < tbox.options.length; i++) {
arrLookup[tbox.options[i].text] = tbox.options[i].value;
arrTbox[i] = tbox.options[i].text;
var fLength = 0;
var tLength = arrTbox.length;
for(i = 0; i < fbox.options.length; i++) {
arrLookup[fbox.options[i].text] = fbox.options[i].value;
if (fbox.options[i].selected && fbox.options[i].value != "") {
arrTbox[tLength] = fbox.options[i].text;
else {
arrFbox[fLength] = fbox.options[i].text;
fbox.length = 0;
tbox.length = 0;
var c;
for(c = 0; c < arrFbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrFbox[c]];
no.text = arrFbox[c];
fbox[c] = no;
for(c = 0; c < arrTbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrTbox[c]];
no.text = arrTbox[c];
tbox[c] = no;

I was just using alert(csi); just to see what it was returning.

Now, if I use just the CheckSelectedItem(), it returns exactly how it should returns false if the date doesn't exist, returns true if it does.) However, once it is added into move(), it will work the first time, but after that it returns undefined. Hopefully I am explaining this thorough enough. If not, let me know.

Where am I going wrong?


8 Years
Discussion Span
Last Post by scrager

Hmm...Why can I only call the CheckSelectedItem() once? That seems to be the issue.


My guess on first look is that there are several return points in your CheckSelectedItem() function has several return points and some of the points don't return a value.

function CheckSelectedItem() {
   for (i = 0; i < len; i++) {
      if (document.workorder_form.employees[i].selected) {
         if (document.workorder_form.employees[i].hasAttribute("rel")) {
            //inArr is true or false at this point
            return inArr;
   //if selected option does not have a rel, then you end up here
   //and the return is undefined

a tip:
instead of looping through all options to find the selected one, you can use selected index:

var select = document.workorder_form.employees;
   var selectedOption = select[select.selectedIndex];

also, you will probably want some more sophisticated date checking because your statement

if (wDate == elArray[a]

will only be true if both dates have the same format. IE: 2/2/09 and 2/02/09 will return false even though they are the same date.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.