2 Replies - 556 Views - Last Post: 31 March 2011 - 05:45 PM

#1 bbert0183  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-March 11

document[buttonname] is undefined

Posted 31 March 2011 - 02:27 PM

Hello, I am somewhat new to java script, I am trying to create a link that when rolled over it changes the image and on roll out it changes back. When I roll over the image it gives me

"Error: document[buttonname] is undefined
Source File: file:///C:/Users/bbertrand/Desktop/testlinks/Untitled-3.html
Line: 18" and the same for line 24.

When i first put the code into the page it worked, but the next day it stopped working.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script language="Javascript" type="text/javascript">
<!--
if (document.images) {
    homebuttonup       = new Image();
    homebuttonup.src   = "homepage-productsfeb.jpg" ;
    homebuttondown     = new Image() ;
    homebuttondown.src = "homepage-customers.jpg" ;
}
function buttondown( buttonname )
{
    if (document.images) {
      document[ buttonname ].src = eval( buttonname + "down.src" );
    }
}
function buttonup ( buttonname )
{
    if (document.images) {
      document[ buttonname ].src = eval( buttonname + "up.src" );
    }
}
// -->
</script>

</head>

<body>

<a onmouseover="buttondown('homebutton')" onmouseout="buttonup('homebutton')" href="http://www.tannisfood.com/index.php?page=newsarticle"><img src="homepage-productsfeb.jpg" border="0" alt="" />This is a Link </a> 

</body>
</html>




Thank you in advance for any help received. Please let me know if you require more information

Is This A Good Question/Topic? 0
  • +

Replies To: document[buttonname] is undefined

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: document[buttonname] is undefined

Posted 31 March 2011 - 04:57 PM

The mistake you made was you're passing homebutton to the functions buttonup and buttondown which means that javascript will be looking for document['homebutton']. That element does not exist. I assigned a name and an id to the img element and passed that name to the functions. Also you really only need to declare a new Image once...not everytime the function is called.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script language="Javascript" type="text/javascript">
<!--
homebuttonup       = new Image();
homebuttonup.src   = "homepage-productsfeb.jpg";
homebuttondown     = new Image() ;
homebuttondown.src = "homepage-customers.jpg" ;

function buttondown( buttonname )
{
    if (document.images) {
      document[ buttonname ].src = homebuttondown.src;
    }
}
function buttonup ( buttonname )
{
    if (document.images) {
      document[ buttonname ].src = homebuttonup.src;
    }
}
// -->
</script>

</head>

<body>

<a onmouseover="buttondown('picture')" onmouseout="buttonup('picture')" href="http://www.tannisfood.com/index.php?page=newsarticle"><img src="homepage-productsfeb.jpg" border="0" alt="" name="picture" id="picture" />This is a Link </a> 

</body>
</html>




Was This Post Helpful? 1
  • +
  • -

#3 bbert0183  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-March 11

Re: document[buttonname] is undefined

Posted 31 March 2011 - 05:45 PM

Thank you very much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1