Hi there, hoping you can help me with a conundrum I have...
What ive made so far is a page header image with 4 buttons to the side of that, which when clicked will change to a diff image (sprite image, triggered by some javascript I found on the net to change the class of the buttons so that the image changes - and it looks toggled).
This works fine and if a 'toggled' button is clicked again then it will delete the class the javascript adds and 'untoggle' it.
The final aim of this is when this page is first loaded then none of these buttons are toggled and a default image in the header is shown, but when one is selected then I want the header image to change to one that is associated with the button that is pressed - I was able to achieve this with an onmouseclick action on the button to change the header image, but if the user then clicked the button again (untoggled it), how would I then be able to change the header image back to the default one?
Could this be achieved by some javascript that changed the class of the header image to the one associated with the button pressed, and then if its pressed again then it clears the header image class?
Not that great with javascript to be honest, any of you guys have some advice? Maybe there are some easier methods ive missed?
Below is the code I have so far, many thanks in advance!
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="styles.css"/>
<script language="javascript" src="java.js"></script>
</head>
<body>
<div id="style">
<div id="styleimage"><img src="images/header.jpg" id="image" width="610" height="229" /></div>
<div id="stylenav">
<ul>
<li id="style1"><a href="#"></a></li>
<li id="style2"><a href="#"></a></li>
<li id="style3"><a href="#"></a></li>
<li id="style4"><a href="#"></a></li>
</ul>
</div>
</div>
</body>
</html>
#style {width:880px; height:229px;}
#styleimage {width:610px; height:229px; float:left;}
#stylenav {width:270px; height:229px; float:left;}
#stylenav ul {list-style: none; padding: 0; margin: 0;}
#stylenav li {float: left;}
#stylenav li a {
height:55px;
float: right;
display: block;
text-decoration: none;
text-align: center;
}
#stylenav li a:hover {background-position:-270px 0px}
#stylenav li.selected a {background-position:-270px 0 !important}
window.onload=function () { setStyles(); };
function setStyles() {
ids = new Array ('style1','style2','style3','style4');
for (i=0;i<ids.length;i++) {
document.getElementById(ids[i]).className='';
document.getElementById(ids[i]).onclick=function() { return Cngclass(this); }
}
}
function Cngclass(obj){
var currObj;
for (i=0;i<ids.length;i++) {
currObj = document.getElementById(ids[i]);
if (obj.id == currObj.id) {
currObj.className=(currObj.className=='')?'selected':'';
}
else { currObj.className=''; }
}
return false;
}