1

We have jquery menu in application, and on selecting of particular element; its background color gets change.

Color is changing according to css. We are designing "Theme" and are trying to change color of css dynamically using Javascript, so that on selecting menu; its color will be changed. But, we are not getting well way to do it.

Following is the css: -

.mmenu li.mmenu-selected > a
{
    background: #1971AA;
    color:#FFFFFF;
    font-weight: normal;
}
2
  • 4
    Create a class in your CSS, and use jQuery's addClass() method to assign it to the elements you require. Commented Jun 29, 2015 at 10:32
  • Stylesheets can be dynamically manipulated - but this is not the appropriate method for this, Commented Jun 29, 2015 at 10:44

2 Answers 2

1

If you have a fixed number of values for the background, then use classes like this:

.theme1 .mmenu li.mmenu-selected > a {background-color: #f99;}
.theme2 .mmenu li.mmenu-selected > a {background-color: #ff9;}
.theme3 .mmenu li.mmenu-selected > a {background-color: #fff;}

If not, use inline styles, targetting the parent and giving an inherit:

$(".mmenu").css("background-color", "#ff9");

And in the CSS:

.mmenu li.mmenu-selected > a {background-color: inherit;}
Sign up to request clarification or add additional context in comments.

3 Comments

Kindly correct me, if I am wrong.Theme 1 to 3, we will define in some css file and on selecting of particular theme, we will load it. And because of inherit property; it would work fine for all selected menu item.But, How will we load into memory?
But, How will we load into memory?
@Worldprogram Add it into CSS file.
0

Try css method:

$(".mmenu li.mmenu-selected > a").css({"background": "#1971AA", "color":"#FFFFFF"});

1 Comment

Note: sets css style on the selected elements; does not change the stylesheet. Also not very 'clean' in terms of adding style decorations.

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.