7

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....

3 Answers 3

9

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+')';
Sign up to request clarification or add additional context in comments.

1 Comment

As you guys usually say if we DUMMIES out here asked the right questions, you could probably give us the right answers.... –
8

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 + ')');

1 Comment

As you guys usually say if we DUMMIES out here asked the right questions, you could probably give us the right answers....
0

I didn't give enough background in my original question...

ie; I forgot to mention that I was attempting to do this in the middle of a server side loop.

I achived what I had set out to do with the following code

`<script type="text/javascript">document.write("<table background=\"http://..../Pages/"+image+"/bkg.jpg\" >");`

Thank you VERY much for your help !!

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.