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

"Object expected" error

Status
Not open for further replies.

kreskin

Technical User
Jul 12, 2004
8
CA
Hello. I m trying to create an expand/hide button. It would be an image that, when clicked, would either expand or hide a table and change the img src from arrow-up to arrow-down or vice versa. I'm getting an "object expected" error in the following code:

<script type="javascript">
function displ(tblID, that)
{
var showhide;
showhide='';
if (that.src=="images/blackD.gif"); //click on down, show table
{
showhide=='block';
up(that);
}
else
{
showhide=='none';
down(that);
}

showhide='block';
window.document.getElementById(tblID).style.display=showhide;
return true;
}

function up(that)
{
imgU = new Image(); imgU.src = "images/blackU.gif";
that.src=imgU.src;
}

function down(that)
{
imgD = new Image(); imgD.src = "images/blackD.gif";
that.src=imgD.src;
}

</script>

<img src="./images/blackU.gif" alt="" width="12" height="10" border="0" onclick="displ('tbl', this);">

<!--A table with id='tbl'-->

I get the error on the img line, when I'm calling the function. Anyone know why that is? Thanks in advance,

Igor.
 
Change script type to "text/javascript".

Also remove ";" after first if() statement - this causes runtime error.
Also #2, I'm not sure checking that.src will work. Browsers usually normalize src to full path. Check it with debbuger or alert() function.
 
Hi. Thanks for the two tips. As for the src, it actually works fine. I checked it with alert and, even though it displays the full path, changing src to /images/smthelse would automatically append the path. The error is still showing...
 

There are many errors... vongrunt has already pointed out 2 of them (script type and trailing ;).

That aside, you're also using "==" instead of "=" to assign values to variables - in 2 places ("showhide=='block';" and "showhide=='none';").

Fix those, and you'll probably have something close to working code.

Hope this helps,
Dan
 
It works now. Here it is:

function displ(that, id)
{
if (that.src==" //click on down, show table
{
window.document.getElementById(id).style.display='block';
up(that);
}
else
{
window.document.getElementById(id).style.display='none';
down(that);
}
return true;
}

So I needed the full path there. I don't need the full path when assigning the src, but when checking it (as in an if statement), I do... Sorry about that and thanks to you both.

P.S. As for the '==', I forgot what the difference between that and '=' is (it matters quite a lot in C++)... I guess I'll go find a refresher.
 
For on/off behaviors it is usually better to rely on target style.display - it makes code less dependent. With checking src stuff, someone (heh, designer) changes image and script stops working. Move code from localhost somewhere else and script stops working etc.
Code:
<script type="text/javascript">
function displ(tblID, that)
{	with( document.getElementById(tblID).style )
		if (display=="none")   //click on down, show table
		{	display="block";
			that.src = "images/blackU.gif";
		}
		else
		{	display="none";
			that.src = "images/blackD.gif";
		}
		
	return true;
}
</script>
 
Thank you vongrunt. I've used your code and it works well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top