<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.
<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>
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>