I have a variable that I can display on the page by using the following
<script>document.write(image)</script>
I can copy the result to the browser and it displays the file I require.
What I want to do is to take that variable and use it to specify the src of my background image
`<p style="background-image: url(**image**);">`
I know that this is probably simple to you guys, but I have spent the whole day and am losing a lot of hair over this one. Thanks for your time....
backgroundImage = "url('image. png')"; You need to put the relative path to the image from your web page's folder. If the image is located in the same folder, then you can reference it directly inside the url() function as in the code above.
Usage is simple — you insert the path to the image you want to include in your page inside the brackets of url() , for example: background-image: url('images/my-image. png'); Note about formatting: The quotes around the URL can be either single or double quotes, and they are optional.
If the element is selectable :
<p id="paragraph">Some text</p>
You can change the style with javascript:
document.getElementById('paragraph').style.background = 'url('+image+')';
if you're creating the paragraph you can do:
var p = document.createElement('p');
p.style.background = 'url('+image+')';
Give the <p>
element an id:
<p id="myPElement">
Manipulate it in the dom...
document.getElementById('myPElement').style.backgroundImage = 'url(' + image + ')';
... or with jquery:
$('#myPElement').css('background-image', 'url(' + image + ')');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With