0

This is the css i'm trying to apply to all the keywords

#showKeywords {
    background-color:#5B1762;
}   

What i want to do is when you click the showKeywords button the keywords are highlighted. However instead of highlighting the keywords it applies the css style to the button. I notice that the id of the button and the name of the css style are the same. How can I change the .keyword class without changing the button (showKeywords) style

 $(document).ready(function() {
    $("#showKeywords").click(function() {
        $(".keyword").addClass("showKeywords");
    });
<div id="menuDiv">
    <span id="showVariables" class="button">Variables</span>
    <span id="showKeywords" class="button">Keywords</span>
</div>
<br>
&nbsp;&nbsp;
<span class="keyword">public</span> 
<span class="keyword">static</span>
<span class="keyword">void</span> main(String args[]) {

I'm not allowed to change the name of the #showKeywords or I would have done that.

2
  • 1
    that isn't a class - that's an id. Commented May 29, 2015 at 1:25
  • You don't actually have a "showKeywords" class in your stylesheet (not one that we can see anyway) Commented May 29, 2015 at 1:29

1 Answer 1

2

Try changing

#showKeywords {
    background-color:#5B1762;
}  

to

.showKeywords {
    background-color:#5B1762;
}

The '#' character in front of showKeywords means that the styles are only shown for an element with an id of 'showKeyWords.' What you want is a CSS class, designated with a period.

EDIT

If you aren't allowed to change the existing CSS, you'll have to add a style tag to your HTML. Put the following code inside that page's <head> tag, or at the top of the file if you're using a layout page.

<style type="text/css">
    .showKeywords {
        background-color:#5B1762;
    }
</style>

And finally, if you would like to prevent the showKeyWords button from being that color as well, just change the id of the showKeyWords button in your HTML and JavaScript to something different.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm not allowed to change the existing css :c
Yea I just added existing css as a class and it works.

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.