1

I'm creating a user signup page and I want the user to be able to enter an image URL and then preview the image in a reserved div (which has a default image in it already, applied by a given class). My HTML for the user image display is:

<div id="user-image-preview" class="col-md-4 col-sm-12 user-image-preview">  </div>
<button id="preview-image">Preview image</button>

The CSS for id'user-image-preview' and class 'user-image-preview' are:

#user-image-preview {
height: 300px;
width: 300px;
border-radius: 50%;
position: absolute;
left: 200px;
top: 150px;
background-size: cover;
}

.user-image-preview {
background-image: url("https://cdn1.iconfinder.com/data/icons/unique-round-blue/93/user-512.png");
background-size: cover;
}

Here is the input part of the form where user inputs image URL:

<div class="form-group">
    <i class="fa fa-picture-o" aria-hidden="true"></i>
    <label for="img">Image</label>
        <input id="user-image-set" type = "text" class ="form-control" placeholder = "Enter image URL" name = "image">
 </div>
 <button id="submit-login" type ="submit" class="btn btn-primary btn-lg  disabled">Signup</button>

And then I wrote the following jQuery to remove the default image (class="user-image-preview") and then add whatever is in the input field:

$("#preview-image").on("click", function() {
var newUserImage = $('#user-image-set').val();
    $("#user-image-preview").removeClass("user-image-preview");
    $("#user-image-preview").css("background-image", "url('newUserImage')");
    console.log(newUserImage)
})

(I added the console.log just to check if I am capturing the variable correctly which I am).

I presume the is a problem with my use of quotes within the URL part of the function, but I can't quite work out what the correct way to cite this variable is.

2 Answers 2

2

As newUserImage is a variable, you don't have to put it in quotes '' so change this line

$("#user-image-preview").css("background-image", "url('newUserImage')");

to

$("#user-image-preview").css("background-image", "url(" + newUserImage + ")");
Sign up to request clarification or add additional context in comments.

1 Comment

Don't you need ' ' around newUserImage ?
0

This solution worked for me:

$("#user-image-preview").css("background-image", "url(' " + newUserImage + "')");

you need to add ''

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.