...

View Full Version : selected is null or not an object



bugfinder
05-17-2009, 08:16 AM
Good day every body i have issue with my script can any one check this for me i use IE 6 and i get
selected is null or not an object error



function movein()
{
var target_list = document.getElementById('target_list');;
var allocated_terminal = document.getElementById('allocatedTerminals');
var target_list_length = target_list.length;
for(var j=0; j<target_list_length; j++)
{
if(target_list[j].selected) //this is where im getting error msg
{
var tmp = target_list.options[j].text;
var tmp1 = target_list.options[j].value;
target_list.remove(j);
j--;
var y=document.createElement('option');
y.text=tmp;
try
{
allocated_terminal.add(y,null);
}
catch(ex)
{
allocated_terminal.add(y);
}
}
}
}

function moveout()
{
var target_list = document.getElementById('target_list');
var allocated_terminal = document.getElementById('allocatedTerminals');
var allocated_terminal_length = allocated_terminal.length;
for(var j=0; j<allocated_terminal_length; j++)
{
if(allocated_terminal[j].selected) //this is where im getting error msg
{
var tmp = allocated_terminal.options[j].text;
var tmp1 = allocated_terminal.options[j].value;
allocated_terminal.remove(j);
j--;
var y=document.createElement('option');
y.text=tmp;
try
{
target_list.add(y,null);
}
catch(ex)
{
target_list.add(y);
}

}
}
}




<tr >
<td align="left" valign="middle" width="30%">
<select name="target_list[]" id ='target_list' size="3" multiple style="width:100 ">
<?
//terminal identification in multi-terminal rooms
$query ="SELECT IP, Room FROM `".$DBTablesPrefix."_stbs`"."WHERE Version='aminet110' "." ORDER BY Room ";
$Infos = mysql_query($query) or die("Query failed on line 188");
$i = 0;
while($Row_Infos = mysql_fetch_array($Infos))
{
$i++;
$IP = $Row_Infos['IP'];
$Room = $Row_Infos['Room'];
$messRoom = Room;
//echo "<option value='".$Room[$i]."' ".$strSelected.">".$Room."::".$IP."\r\n";
echo "<option value='".$Room[$i]."' ".$strSelected.">$messRoom ".$Room." \r\n";
}
?>
</select>
</td>
<td width="10%" align="center" valign="middle">
<input type="hidden" name="action" value="add">
<table border="0">
<tr align="center" height="50%">
<td width="30%">
<?
echo '<img src="../admin/img/right.gif" border="0"><img src="../admin/img/right.gif" border="0">';
?>
</td>
<td>

<input type="button" onClick='movein()' value="Add">

</td>
<td width="30%">
<?
echo '<img src="../admin/img/right.gif" border="0"><img src="../admin/img/right.gif" border="0">';
?>
</td>
</tr>
<tr align="center" height="50%">
<td width="30%">
<?
echo '<img src="../admin/img/left.gif" border="0"><img src="../admin/img/left.gif" border="0">';
?>
</td>
<td>
<input type="button" onClick="moveout()" value="Remove">
</td>
<td width="30%"><? echo '<img src="../admin/img/left.gif" border="0"><img src="../admin/img/left.gif" border="0">';?>
</td>
</tr>
</table>
</td>

<td align="right" width="30%">
<select name="allocatedTerminals[]" id='allocatedTerminals' size="3" multiple style="width:100 " >

</select>
</td>
</tr>

adios
05-17-2009, 09:30 AM
So you did this -

var tmp = target_list.options[j].text;
var tmp1 = target_list.options[j].value;

... properly checking each option object ... but before that. you did:

if(target_list[j].selected)

... checking for a 'selected' property of the Select object. No such thing; there is a 'selectedIndex' property, but with a multiple, you'll just get the first. Iterate over the options:

if(target_list.options[j].selected)

You php people & your square brackets .... :rolleyes:

Just for the record, Select[n] is OK, the equivalent of Select.options[n]. Typical DOM shorthand, stupid of me to forget it.

bugfinder
05-17-2009, 10:27 AM
thanks for ur reply im not a Core JS programmer ... im trying out with some of examples on the internet ...!!!
I still get the same error ..!! Any suggestion..??

bugfinder
05-17-2009, 03:02 PM
What you told didn't work :confused:

bugfinder
05-17-2009, 03:04 PM
i Get this error now
Error:-options[...] is null or not an object

:confused:

adios
05-17-2009, 07:44 PM
1) View source on the page in question and post it here.
2) Or: put it on your server and post a link.

bugfinder
05-18-2009, 07:24 AM
here is the code u asked for ...



<form name='form_allocate_terminals'>
<table border="0" cellpadding="4" cellspacing="4" width="50%" align="center" class="tableForm">
<tr >

<td align="left" valign="middle" width="3%" >
<font color="#000000"><strong>Type:</strong></font>
</td>
<td align="left" valign="middle" colspan="5">
<select name="message" >
<option value='43' >Fire<option value='44' >Attention </select>
</td>
</tr>
<tr bgcolor="#ECECEC" >
<td align="left" valign="middle" width="3%" colspan="4">
<input type="radio" name="target_type" value="all" checked onClick="disableItems(this);">
<font color="#000000" size="2"><strong>All</strong></font>
</td>
</tr>
<tr bgcolor="#ECECEC">
<td align="left" valign="middle" width="3%" colspan="4">
<input type="radio" name="target_type" value="room" onClick="disableItems(this);">
<font color="#000000" size="2"><strong>Rooms</strong></font>
</td>
</tr>
<tr >
<td align="left" valign="middle" width="30%">
<select name="target_list[]" id ='target_list' size="3" multiple style="width:100 ">
<option value='6' >Room 663
<option value='' >Room 99
</select>
</td>
<td width="10%" align="center" valign="middle">
<input type="hidden" name="action" value="add">
<table border="0">
<tr align="center" height="50%">
<td width="30%">
<img src="../admin/img/right.gif" border="0"><img src="../admin/img/right.gif" border="0"> </td>
<td>

<input type="button" onClick='movein()' value="Add">

</td>
<td width="30%">
<img src="../admin/img/right.gif" border="0"><img src="../admin/img/right.gif" border="0">
</td>
</tr>
<tr align="center" height="50%">
<td width="30%">
<img src="../admin/img/left.gif" border="0"><img src="../admin/img/left.gif" border="0"> </td>
<td>
<input type="button" onClick="moveout()" value="Remove">
</td>
<td width="30%"><img src="../admin/img/left.gif" border="0"><img src="../admin/img/left.gif" border="0"> </td>
</tr>
</table>
</td>

<td align="right" width="30%">
<select name="allocatedTerminals[]" id='allocatedTerminals' size="3" multiple style="width:100 " >

</select>
</td>
</tr>
<tr align="center" >
<td colspan="4"><input type="button" name='btnSend' value="Send" onClick="sendemergency(form_allocate_terminals)">
<!-- <input name="btnsend" type="button" class="buttform" id="34" value="Send Emergency" onclick="pushpage()" />--></td>
</tr>
</table>
</form>
<script language="javascript">
var cmdLink = "EMERGENCY_execcommand.php?Emergency=Attention&targetType=all&ip=10.11.1.4";
function pushpage()
{
Link = cmdLink+" SEND";
//alert(Link);
var msg = "Are you sure you want to delcare Emergency?";
if(confirm(msg))
document.location.href=""+Link;
}
function movein()
{
var target_list = document.getElementById('target_list');;
var allocated_terminal = document.getElementById('allocatedTerminals');
var target_list_length = target_list.length;
for(var j=0; j<target_list_length; j++)
{
if(target_list.options[j].selected)
//if(target_list[j].selected) //this is where im getting error msg
{
var tmp = target_list.options[j].text;
var tmp1 = target_list.options[j].value;
target_list.remove(j);
j--;
var y=document.createElement('option');
y.text=tmp;
try
{
allocated_terminal.add(y,null);
}
catch(ex)
{
allocated_terminal.add(y);
}
}
}
}

function moveout()
{
var target_list = document.getElementById('target_list');
var allocated_terminal = document.getElementById('allocatedTerminals');
var allocated_terminal_length = allocated_terminal.length;
for(var j=0; j<allocated_terminal_length; j++)
{
if(allocated_terminal.options[j].selected)
if(allocated_terminal[j].selected) //this is where im getting error msg
{
var tmp = allocated_terminal.options[j].text;
var tmp1 = allocated_terminal.options[j].value;
allocated_terminal.remove(j);
j--;
var y=document.createElement('option');
y.text=tmp;
try
{
target_list.add(y,null);
}
catch(ex)
{
target_list.add(y);
}

}
}
}

function disableItems(selectedItem)
{
if (selectedItem.value =='all')
{
document.getElementById("target_list").disabled = true;
document.getElementById("allocatedTerminals").disabled = true;

}
else if(selectedItem.value =='room')
{
document.getElementById("target_list").disabled = false;
document.getElementById("allocatedTerminals").disabled = false;

}
}
window.onload=function()
{
var targetType = 'all';
if (targetType =='all')
{
document.getElementById("target_list").disabled = true;
document.getElementById("allocatedTerminals").disabled = true;

}
else if(targetType =='room')
{
document.getElementById("target_list").disabled = false;
document.getElementById("allocatedTerminals").disabled = false;

}
}


function sendemergency()
{
var len =document.form_allocate_terminals.target_type.length;
for (i=0;i<len;i++)
{
if(document.form_allocate_terminals.target_type.checked)
{
alert("Do you really want to send emergency");
}
}
}
</script>
:(

adios
05-18-2009, 08:59 AM
When you decrement the loop counter:

j--;

... you'll also need to decrement the counter limit:

target_list_length--
j--;

... and in the other function:

allocated_terminal_length--;
j--;

... otherwise you'll go out of bounds. As I said, these:

if(target_list.options[j].selected)
if(allocated_terminal.options[j].selected)

... are correct.

One other thing - radio buttons tend to have persistence, so you might want to run the onclick handler of the appropriate 'target_type' radio each time the page loads:

window.onload = function()
{
var target_type = document.form_allocate_terminals.elements['target_type'];
for (var i = 0; i < target_type.length; i++)
{
if (target_type[i].checked)
{
target_type[i].onclick();
}
}
}

Keeps things in sync ...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum