Set background image in CSS using jquery

css, html, javascript, jquery

Solution

Try this:

<div class="rmz-srchbg">
    <input type="text" id="globalsearchstr" name="search" value="" class="rmz-txtbox">
    <input type="submit" value="&nbsp;" 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>

Problem

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="&nbsp;" 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.

Original source

Related problems