0

I need a java script code to change the color of menu on Onclick event . I am using xhtml for the UI(rendered to html) .when I click on the particular menu ,its color should change to any other color.

I searched for the answers but , they are based on URL change .In my case the URL will remain the same .

Below sample javascript code for changing the color of the menu ,but it doesn't change the color of the inactive menus .Any easy way to do this?

MyXhtml-

<ice:panelGroup>
                    <li id="liHome" class="menu" onClick="changeColor(this);">
                        <ice:commandLink actionListener="#{menuBean.navigateToHomePage}" style="width: 34px;"
                            styleClass="curveLeft" id="lnkHome" name="lnkHome">
                            #{msgs['itrigger.menu.homePage.label']}
                        </ice:commandLink>
                    </li>
                </ice:panelGroup>

Javascript

if (document.forms[2]) {

        var formId = document.forms[2].id;
        if(formId == 'errorMessagesForm') {
            formId = document.forms[3].id;
        }
        var selectedMenuObj;
        var selectedMenuLnkObj;
        if (formId) {
            if (formId == 'homeform') {
                document.getElementById('lihome').style.background = "lightgrey";
                document.getElementById('lihome').className = "selectedMenu";
            } else if ((formId == 'resourcesForm')) {
                document.getElementById('liResources').style.background = "lightgrey";
                document.getElementById('liResources').className = "selectedMenu";

        }
    }

CSS class

ddsmoothmenu ul li a.selectedMenu:link {
    font-family: arial narrow;
    color: #717171;
    padding: 4px 20px 6px 10px;
    text-decoration: none;
    letter-spacing:1px;
    background-image:url(../images/Left_lightgrey_Menu-Tabs.png);
    background-repeat:no-repeat;
    background-position:left;
    width: 40px;    
    font-weight: bold;
}
2
  • Did it get into the if statement? Commented May 15, 2016 at 7:56
  • @user26409021- it is going into if statement Commented May 15, 2016 at 8:02

1 Answer 1

1

Do something like this:

<item class="item" onClick="changeColor(this);">Home</item>
<item class="item" onClick="changeColor(this);">Page 2</item>

<script>
function changeColor(elem){

var all = document.getElementsByClassName('item');

for(var i=0;i<all.length;i++){
all[i].style.background = 'inactive color';
}

elem.style.background = 'active color';

}
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

Hi .. Actually it is not working completely ..On first click the other menus changes to inactive color and the one selected becomes active . But when I click on the other menu , the one already selected does not change color to inactive .
Could you post something on jsFiddle?
I am using a lot from java side and different js files . But for info ,I am using IE11 for testing. If you need any particular info?? ..I can share that with you.
Please find the current js and css which is getting used . This is currently not working .
Maybe it doesn't work because you have set background things in css class. Maybe it will work if you disable these.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.