I am trying to set the background image for one of my html element using jquery
<div class="rmz-srchbg">
<input type="text" id="globalsearchstr" name="search" value="" class="rmz-txtbox">
<input type="submit" value=" " id="srchbtn" class="rmz-srchico">
<br style="clear:both;">
</div>
$("#globalsearchstr").focus(function(){
$(this).parent().css("background", "url(/images/r-srchbg_white.png) no-repeat;");
});
but this never works.On focus only change happens is that a style attribute is added to HTML, like this
<div class="rmz-srchbg" style="">
</div>
No change in CSS happens.
Try this:
<div class="rmz-srchbg">
<input type="text" id="globalsearchstr" name="search" value="" class="rmz-txtbox">
<input type="submit" value=" " id="srchbtn" class="rmz-srchico">
<br style="clear:both;">
</div>
<script>
$(function(){
$('#globalsearchstr').on('focus mouseenter', function(){
$(this).parent().css("background", "url(/images/r-srchbg_white.png) no-repeat");
});
});
</script>
Use :
$(this).parent().css("background-image", "url(/images/r-srchbg_white.png) no-repeat;");
instead of
$(this).parent().css("background", "url(/images/r-srchbg_white.png) no-repeat;");
More examples you cand see here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With