1

I have a problem when i try to add CSS attributes to this class it gives me an error

My code

$('.openerp .nav-pills > li.active a:hover, .openerp .nav-pills > li.active a:focus, .openerp a.list-group-item.active a:hover, .openerp a.list-group-item.active a:focus').css({'color': res["left_hover_font_color"],'background-color': res["left_hover_bg_color"]});

error:

Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: hover

Why hover causing a problem here?

6
  • What's res["left_hover_font_color"]? Commented Apr 12, 2016 at 17:17
  • @j08691 An array. Though I would have made it into an object and call it as res.left_hover_font_color, but it's a choice. Commented Apr 12, 2016 at 17:19
  • do you have any jsfiddle for this ? Commented Apr 12, 2016 at 17:20
  • @ Babydead @j08691 Yep i use dict Commented Apr 12, 2016 at 17:24
  • you are using a:hover filter in the JQUERY select statement while a:hover is not a valid filter Commented Apr 12, 2016 at 17:40

2 Answers 2

1

JQuery selectors are similar but not exactly the same as css selectors if you want to target an event such as hover or focus you need to use a callback for example:

$('.openerp .nav-pills > li.active a').hover(function() {
    $(this).css({'color': res["left_hover_font_color"],'background-color': res["left_hover_bg_color"]});
},function() {
    $(this).css({'color': "",'background-color': ""});
});
Sign up to request clarification or add additional context in comments.

1 Comment

I have a problem with that code i don't know why ,,, when i go and select any menu of them and i reselect another on ,,,the first lose the hover and the text disappear
1

:hover is not a DOM element but a status. The css() function of jquery uses inline-css. inline-css can not be applied to a non-existing element. If you want to change your :hover class's effect, I would using .addClass() to add a class such as alternative-over and style it with css.

A way to do it with pure jQuery could also be to say:

$('a').on("hover", function(){
  $(this).css({your styles});
});

A third option is to add css between <style> tags in your DOM through jQuery, though I wouldn't recommend it as it gets messy.

edit: I will try to make an example with your specific code as you've requested, but I'm doing this blindly right now and cannot guarantee it will work right off the bat. A little tweaking might be necessary

//Specify the parents
var obj = $('.openerp .nav-pills > li.active, .openerp .nav-pills > li.active, .openerp a.list-group-item.active, .openerp a.list-group-item.active');

//This makes it grab the "a" within the objects(parents) you've specified before
    $('a', obj).on("hover", function(){
//This is what happens when you enter the area with your mouse
      $(this).css({'color': res["left_hover_font_color"],'background-color': res["left_hover_bg_color"]});
    },
function(){
  //this is what happens when your mouse leaves the area (removes the :hover)
  $(this).attr("style", ""); //resets the inline-styling
});

^ The above is a pure jQuery way. I myself would use css to get what you want. Adding and removing the class, like so;

//Specify the parents
var obj = $('.openerp .nav-pills > li.active, .openerp .nav-pills > li.active, .openerp a.list-group-item.active, .openerp a.list-group-item.active');

//This makes it grab the "a" within the objects(parents) you've specified before
    $('a', obj).on("hover", function(){
//This is what happens when you enter the area with your mouse
      $(this).addClass("alternative-hover");
    },
function(){
  //this is what happens when your mouse leaves the area (removes the :hover)
            $(this).removeClass("alternative-hover");
});

4 Comments

could you apply what you say to my example or code please for better understanding
Both doesn't work :( i tried the first one which i would believe it should work but it didn't work
Could you try and remove the other options from the "var obj"? So, for now, only take 1? `var obj = $('.openerp .nav-pills > li.active'); also, are you getting any (new) errors? In that case you should let me know, it would make a big difference
I see now that I had a typo in my code. If you tried to blindly copy-paste it without reading it over, I feel like you're going to hit trouble and keep asking questions. You should try to learn from the answers you get. I'm glad someone elses answer worked for you, but I also predict issues with that one if you don't do anything with the answer. Good luck, anyway.

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.