<p class="text">Some text</p>
<style>
.text:hover{
color:red;
}
</style>
How can I trigger hover event that text becomes red via js or jquery? I need a solution without adding a new css class.
You need to use hover() function of JQuery with syntax hover( handlerIn, handlerOut ) as you cannot manipulate :hover from JQuery.
$('.text').hover(
function(){
$(this).css('color', 'red');
},
function(){
$(this).css('color', 'black');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="text">Some text</p>
I need a solution without adding a new css class.Try This:
$(document).ready(function(){
$('.text').hover(function(){
$(this).css('color','red');
},function(){
$(this).css('color','initial');
})
})
$(document).ready(function(){
$('.text').hover(function(){
$(this).css('color','red');
},function(){
$(this).css('color','initial');
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<p class="text">Some text</p>
I need a solution without adding a new css class.It's not possible then. You cannot invoke the CSS:hoverselector from JS. See this specific answer in the duplicate I marked for more details: stackoverflow.com/a/17244507/519413