Here's what I'm trying to achieve: I have four buttons in a list and each button has a white background and a unique colour border. When one button is clicked its background becomes the same colour as its border. When a second button is clicked the first button returns to normal and the second buttons background gets filled with the second buttons border colour. Each button has the id "navX" where X is a number from 1 to 4.
I have been using a mix of jQuery and javascript to achieve this. I was trying to use jQuery on click to set all button backgrounds to white and trying to use javascript to fill in the clicked buttons background. This is because I know jQuery allows you to gather all elements with a common id string:
$('[id^=nav]').css({"background":"#FFFFFF", "color":"#000000"});
whilst with javascript I can pass the clicked id and a colour parameter to the function:
<a id="nav1" onclick="changeHeaderColour(this, '#f0e442')"> Button 1 </a>
function changeHeaderColour(navItem, newColor) {
document.getElementById(navItem.id).style.backgroundColor = newColor;
document.getElementById(navItem.id).style.color = newColor;
}
I have been playing around with mixtures of ways of combining these, varying which selectors to use, and tampering with the core CSS and I am stuck achieving one of two things:
- When a button is clicked, it gets stuck permanently with a filled in background. Continuing to click buttons finishes with all buttons stuck filled in.
- When a button is clicked, all buttons get stuck permantently with a white background.
I really have no idea how else to achieve this. I just can't seem to get the hang of finding the correct mix of CSS levels that don't override each other. I haven't used jQuery's addClass() method since each class needs a unique colour. If anyone has any advice at all that would be great - it seems like a simple task and I was determined to achieve it on my own but I have been going at this for hours now!
Thanks for any help!
document.getElementById(navItem.id),navItemwill already be the element you are passing.