1
<p class='sent' id='abc'>abc</p>
<p class='sent' id='abb'>abb</p>
<p class='sent' id='acc'>acc</p>

css

.sent:hover{
background-color:#e1e1e1;  // works until the first click on .sent
}

js

$(".sent").click(function() {
$('.sent').css('background-color', 'transparent'); //works
$(this).css('background-color', '#e1e1e1');  //works
});

After first click on sent css sent:hover doesnt work !?

3
  • 2
    Because you've set the default colour to be the hover colour, so obviously there will be no visual change. Commented Oct 15, 2013 at 8:54
  • 1
    That's because you are setting an inline-style. Commented Oct 15, 2013 at 8:54
  • 1
    @danrhul I think there's multiple .sent elements, so he wants the current chosen or the hovering element to have colour #e1e1e1. Commented Oct 15, 2013 at 8:55

4 Answers 4

2

Inline styles have precedence over rules defined in style blocks.

Try removing the background-color style instead of setting it to transparent:

$(".sent").click(function() {
    $(".sent").css("background-color", "");
    $(this).css("background-color", "#e1e1e1");
});
Sign up to request clarification or add additional context in comments.

Comments

2

Try .one()

<p class='sent' id='abc'>abc</p>
<p class='sent' id='abb'>abb</p>
<p class='sent' id='acc'>acc</p>

js

$('.sent').one( 'click',function(){
    $(this).addClass('sent_clicked');  
});

or

$('.sent').one( 'click',function(){
    $('.sent').addClass('sent_clicked');  
});

css

.sent_clicked:hover{
background-color:#e1e1e1; 
}

1 Comment

This will not unset other .sent elements which seems to be what OP wants.
1

Comment out

$('.sent').css('background-color', 'transparent');

Comments

0

Adding !important to your CSS would do the trick.

.sent:hover{
background-color:#e1e1e1 !important;
}

This is a quick and easy fix. You should avoid using !important in your CSS.

1 Comment

Whilst this would work, it is inherently bad practice. If you feel the need to use !important it usually means your CSS is bad.

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.