Change background color of an element conditionally by JQuery
This post will help you to get value from p element and conditionally change the background color of that element if have value more than 20.
HTML==>
<p class="changebg">1234567891011</p>
<p class="changebg">1</p>
<p class="changebg">60</p>
JQuery==>
<script type="text/javascript">
$(document).ready(function(){
$(".changebg").each(function(){
var count =parseInt($(this).text().trim());
if(count>20){
$(this).css('background-color','#f00');
}
else{
$(this).css('background-color','#ff0');
}
})
});
For complete code reference and Demo, download file from the below link provided.
HTML==>
<p class="changebg">1234567891011</p>
<p class="changebg">1</p>
<p class="changebg">60</p>
JQuery==>
<script type="text/javascript">
$(document).ready(function(){
$(".changebg").each(function(){
var count =parseInt($(this).text().trim());
if(count>20){
$(this).css('background-color','#f00');
}
else{
$(this).css('background-color','#ff0');
}
})
});
For complete code reference and Demo, download file from the below link provided.
![]() |
Comments
Post a Comment