1

I am trying to change the background image of a div with Jquery and its not working

here is the html and jquery

<div class = "displayUser" id = "cover">
<script type="text/javascript">

$(document).ready(function () {
  $('.displayUser').css("background-image", "url(../images/group_icon.png) no-repeat;");
});


</script>

</div>

here is my css

.displayUser{
    height: 600px;
    width: 100%;
    background-repeat: no-repeat;
    background-size: 100%;

}

Why isnt it working?

3 Answers 3

4

You need to split css rules, removing no-repeat; from jQuery code, then it works well.

To do this, you must write two css rules and you can do it using an object as argument.

$(document).ready(function () {
  $('.displayUser').css({
    'background-image': "url(../images/group_icon.png)',
    'background-repeat' : 'no-repeat',
  });
});
Sign up to request clarification or add additional context in comments.

Comments

3

two things:

<div class="displayUser" id="cover">

and:

$('.displayUser').css("background-image", "url('../images/group_icon.png')");
  1. remove the whitespaces before/after id and class attributes.
  2. and remove the no-repeat; from the script and put single quotes in url('').

Comments

0

your url needs to be quoted like this

url('../images/group_icon.png')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.