Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set background image in CSS using jquery [duplicate]

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.

like image 334
None Avatar asked Dec 03 '13 11:12

None


2 Answers

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>
like image 90
Ringo Avatar answered Sep 17 '22 04:09

Ringo


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

like image 41
Aditzu Avatar answered Sep 19 '22 04:09

Aditzu