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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object required error

Status
Not open for further replies.

bthumber

Programmer
Jul 20, 2010
2
US
I'm getting an error on the page during runtime in my javascript: Object required, I can't fine it. Please see code:

view plaincopy to clipboardprint?
function SendTO(){
var toTxtBox = document.getElementById("btnTo");
var NameList; // error is on this line
var count = document.getElementById("lstNDisplay").length;

for(var i=0; i < count; i++){
NameList = document.getElementById("lstNDisplay").options.text;
toTxtBox.value = toTxtBox.value = NameList;
if((count > 1) && (i < count - 1)){
toTxtBox.value = toTxtBox.value + ';' ;
}
}
Cancel();
}

and the button that fires this code:

<input id="OkButton" type="button" value="Ok" onclick="SendTO()" />

function SendTO(){
var toTxtBox = document.getElementById("btnTo");
var NameList; // error is on this line
var count = document.getElementById("lstNDisplay").length;

for(var i=0; i < count; i++){
NameList = document.getElementById("lstNDisplay").options.text;
toTxtBox.value = toTxtBox.value = NameList;
if((count > 1) && (i < count - 1)){
toTxtBox.value = toTxtBox.value + ';' ;
}
}
Cancel();
}

and the button that fires this code:

<input id="OkButton" type="button" value="Ok" onclick="SendTO()" />
The error the runtime page:

Line: 142, char 17, error: Object required
 
Also, your code implies you have more than one element with the same ID ("lstNDisplay")... this is not valid, and you should recode your markup to avoid this.

If it's an array of checkboxes / radio buttons, use getElementsByName instead.

Basically, ID attributes should ALWAYS be unique.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
Yes, btnTo exist and lstNDisplay exist and there is only element one of each. Seems like the code display twice.
 
If there's only ever 1 element with the ID of "lstNDisplay", then why are you doing this:

Code:
var count = document.getElementById("lstNDisplay").length;

for(var i=0; i < count; i++){

If there is only ever 1, then count will always be 1, negating the need for a loop.

Perhaps you can show us your HTML, or better still a URL to the page, as I don't think you're telling us everything.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top