Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Nurbek Ismailov
Nurbek Ismailov
2,068 Points

this code is not running, please help me debug it.

<head> <script type="text/javascript"> var image_tracker='f'; function change () { var image=document.getElementById('social'); if (image_tracker==='f') { img.src='twitter.png'; image_tracker='t'; } else { img.src='facebook.png'; image_tracker='f'; } }; </head> </script> <body> <img src='facebook.png' alt="social logo" id='social' onclick="change();"> </body>

3 Answers

Nathan Ward
Nathan Ward
7,907 Points


You are setting the image to a variable called 'image' but then changing the src using the variable 'img' this could be your problem. Here is an updated version of your javascript:

var image_tracker = 'f';
function change() {
  var image = document.getElementById('social');
  if (image_tracker === 'f') {
    image.src = 'twitter.png'; 
  } else {
    image.src = 'facebook.png'; 
    image_tracker = 'f'; 
Nurbek Ismailov
Nurbek Ismailov
2,068 Points

Nathan, thank for catching it, that was valid error, however it still not working. the images take up the the whole page and may be that is why when i click on it it is not changing from facebook to twitter and vice versa.

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

In your html above you're closing your head tag before you close the script tag:

</head> </script>

Try this instead:

</script> </head>
Nurbek Ismailov
Nurbek Ismailov
2,068 Points

thank you guys, you both debugged it.