16

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.

3
  • what about using absolute path? Check in console if your relative path is accurate Commented Dec 3, 2013 at 11:47
  • CSS $(this).css('background-image', 'url(' + imageUrl + ') no-repeat'); Commented Dec 3, 2013 at 11:53
  • this wont work background-image is wrong it should be backgroundImage Commented Dec 3, 2013 at 11:55

7 Answers 7

22

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>
Sign up to request clarification or add additional context in comments.

Comments

10

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

3 Comments

to use background-image in jquery, you should use backgroundImage !
both methods are accepted.
4

try this

 $(this).parent().css("backgroundImage", "url('../images/r-srchbg_white.png') no-repeat");

1 Comment

Thanks for sharing this. +1 for '' in url(). This works like a charm.
2

Try this

$("#globalsearchstr").focus(function(){
    $(this).parent().css("background", "url('../images/r-srchbg_white.png') no-repeat");
});

2 Comments

Not working.Still same problem(explained in question)
@Athul try now, what is error?
1

Remove the semi-colon after no-repeat, in the url and try it .

$("#globalsearchstr").focus(function(){
    $(this).parent().css("background", "url(/images/r-srchbg_white.png) no-repeat");
});

1 Comment

"background" property is not correct. "background-image" you wanna say.
1

You have to remove the semicolon in the css rule string:

$(this).parent().css("background", "url(/images/r-srchbg_white.png) no-repeat");

Comments

1
<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>
$(document).ready(function() {
  $('#globalsearchstr').bind('mouseenter', function() {
    $(this).parent().css("background", "black");
  });
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.