
Originally Posted by
ketanco
Hi please see
robofight.com/test/
when i click a button to go to a page, i want that button to stay highlighted. (so whichever page i am on, i want that button highlighted). how can I do that?
I'm new to Javascript and I've got something working great. Here is the basic concept on how I do what you are asking.
All my buttons are divs.
Code:
var button1 = document.createElement('div');
var button2 = document.createElement('div');
var button3 = document.createElement('div');
when I want to highlight one of those I just set the sytle different, like it's background image or it's background color this way:
Code:
button2.setAttribute("style", "background-image:highlightedButton.png")
All the others get the regular button png this:
Code:
button2.setAttribute("style", "background-image:button.png")
I add them to a main div using appendChild like this:
Code:
mainDiv.appendChild(button2)
Then I finally add the main div to the body which causes the page to be drawn:
Code:
body.appendChild(mainDiv)
Now, for keeping the page aligned with a button, like you want. I attach my event to buttons they know the page they turn to because of this code: ( you must add the events before you add mainDiv to body)
Code:
button.setAttribute("onclick", "changePage(buttonsPage)")
When the mainDiv is added to the body element the page is redrawn and the highlighted button gets a different background. It's event is set to specific page.
The changePage function actuall removes the old main div and adds the new div you create each time there is a button click. It looks something like this:
Code:
function changePage(highlightedPage)
{
var oldButtonBar = document.getElementById("buttonBar");
var newButtonBar = buttonBarDivFunction("buttonBar", highlightedPage) // events added inside this div function
newButtonBar.appendChild(mainDiv(newButtonBar))
var body = document.getElementsByTagName("body")[0];
body.removeChild(oldButtonBar);
body.appendChild(newButtonBar);
}
Bookmarks