6
I have this function that changes the src of an img:
window.document.images['img'].src = n_src;
<img src="" name="img"/>
But I wanted to change the background url of the style:
<div name="img" style="background: url()"></div>
How best to do this function?
I only had a doubt the link of my image came from another function that was n_src; how can I make this change?
– Josimara
@Josimara in that case you can do
elemento.style.backgroundImage = 'url(' + n_src + ')';
– Sergio
I tried that way before I didn’t get it
– Josimara
Document.querySelector("[name='img']").elemento.style.backgroundImage = 'url('+n_src+')';
– Josimara
@Josimara what is inside
n_src
? what givesalert(n_src);
?– Sergio
@Josimara withdraws
elemento
of that code you put on top... it should be justdocument.querySelector("[name='img']").style.backgroundImage = 'url('+n_src+')';
– Sergio
in n_src this is the link of an image
– Josimara
I’m not getting it yet, but in your example jsfiddle it worked.
– Josimara
If I put the image link right in there this going but if I put the +n_src+ no longer takes.
– Josimara
@Josimara, where are you leaving that variable? Can you put some more code on it so I can figure out where you are?
– Sergio
@josimara, did you try to remove the HTML style attribute as I recommended? If you have taken, copy and paste exactly how you created your variable in the question, please...
– Rafael Kendrik