0

I am trying to add class using JQuery to a button when it is clicked. The following codes show my class and JQuery onclick event with its handler:

CSS

.input-highlight {
     background-color: #1E90FF;
     color: white;
}

JS

$("#elv-geo").on("click", hightlight);

function hightlight() {

    var georgian = $("#geo-elv");

    georgian.addClass("input-highlight");

}

When I had "visibility: hidden" in the class, it did work, but with the above code, it doesn't. Any ideas why?

3
  • 1
    @Amit what would that change? Commented Jun 27, 2013 at 11:37
  • @Amit is there any benefit to wrapping highlight in an anonymous function instead of passing it directly? Commented Jun 27, 2013 at 11:38
  • @JanDvorak is right! no need for a defined fucntion. Commented Jun 27, 2013 at 11:40

2 Answers 2

1

You used once $("#geo-elv") and the other time $("#elv-geo"). This may cause your problem..

Also, you should consider using the following syntax:

$("#geo-elv").on('click',function(){
    $(this).addClass('input-highlight');
})
Sign up to request clarification or add additional context in comments.

4 Comments

So what's the problem, he used one click ID and another for change class ID.
Didn't you read? "I am trying to add class using JQuery to a button when it is clicked"
Yotam, I did confuse id-s and although the class is added to that button when clicked (seen in browser inspect element), it doesn't do what the class says (change background color etc.). I tried using $(this) too, but no success.
Your css might be overridden by some other rules.. try background-color: #1E90FF !important; or changing the location of the rule in the page for it to work. also try to use the rule with the selector: #geo-elv.input-highlight which is more specific thus gets higher priority.
0
.input-highlight {
    background-color: #1E90FF;
    color: white;
}

var id = "#elv-geo"; // is it elv-geo or geo-elv?
$(id).on("click", highlight);

function highlight() {
    $(id).addClass("input-highlight");
}

Comments

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.