0

I am looking to change a value in a tag with js.

<div class="class1" style="background-image:url(https://www.awebsite.com/sites/default/files/a_image.jpg?t=1529620960)"></div>

I want to change this :

https://www.awebsite.com/sites/default/files/a_image.jpg?t=1529620960`

to this:

https://hello.com/an-other-image.jpg

Thanks !

6
  • Your question seems unclear.On what action do you want to replace ? Is it on click of button or something? Commented Sep 21, 2018 at 16:22
  • 3
    and your attempt is... Commented Sep 21, 2018 at 16:22
  • 2
    Possible duplicate of how to change the background image of div using javascript? Commented Sep 21, 2018 at 16:26
  • document.getElementById(id).style.property = new style Commented Sep 21, 2018 at 16:26
  • It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the FAQ and How to Ask. Commented Sep 21, 2018 at 16:29

3 Answers 3

4

You can use document.querySelector to get a reference to the <div> element and then update it's style attributes.

// querySelector can use CSS selectors to find the element in the document
// note: this is assuming there is only 1 element on page with this class
var myDiv = document.querySelector('.class1');
// Style properties generally match CSS but camel cased
myDiv.style.backgroundImage = 'url("https://hello.com/an-other-image.jpg")';

You may want to consider adding an id in which case you can use an ID selector or document.getElementById.

If there are multiple elements you want to update, you can use document.querySelectorAll which will return an array of matches which you can loop over and change.

function changeToAnotherBgImage(element) {
    element.style.backgroundImage = 'url("https://hello.com/an-other-image.jpg")';
}
// Plenty of alternate ways of writing, arrow function may be appropriate
document.querySelectorAll('.class1').forEach(changeToAnotherBgImage);
Sign up to request clarification or add additional context in comments.

3 Comments

What if the class is applied to multiple elements? Not that the OP thought about that.
@evolutionxbox Added a note about querySelectorAll - good calloout :)
It works well ! Thank you for taking the time to help me, I really learned something. I think I will take JS lessons, I did not think we could do this type of things (querySelector and the .style.backgroundImage)
4

You can do something like:

c = document.getElementsByClassName('class1');
for(i = 0; i < c.length; i++){
    c[i].style.backgroundImage = 'url(https://hello.com/an-other-image.jpg)';
}

2 Comments

I agree with this solution. I just tested it and it worked for me.
This works but is missing var or let for variable declarations. forEach is also widely supported and removes the tedium of an index variable. Really el.style.backgroundImage is the core part anyway, but just fyis that may be helpful :)
0

You can do that:

const eleDiv =document.querySelector('.class').style.background = "url('your imge') no-repeat";

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.