0

I need to change the backgroundImage of a div in a parent iframe.

I can do it, but not in a useful way, I need to send the variable from the button.

At the moment I have the variable in the script.

<script>
function myFunction() {
window.parent.document.getElementById('seekerBase').style.backgroundImage = "url('http://www.floom.com/images/waveform_lotos.gif')";
 }
</script>

<button type="button" onclick="myFunction()">Get the background image of div</button>

I need to send the image url from the button and not having it in the script.

I tried this and is not working.

<script>
    function myFunction() {
    window.parent.document.getElementById('seekerBase').style.backgroundImage = "url()";
     }
    </script>

    <button type="button" onclick="myFunction('http://www.floom.com/images/waveform_lotos.gif')">Get the background image of div</button>

Thanks for your help. I am a newbie.

1
  • 2
    Pass it as a parameter function myFunction(img) { ... } Commented Nov 25, 2015 at 17:57

1 Answer 1

4

Background image URL, http://www.floom.com/images/waveform_lotos.gif , passed to myFunction(bgImgUrl) is basically a parameter to the function. For more information on JavaScript Functions, refer Mozilla/Functions

Try this!

<script>
  function myFunction(bgImgUrl) {
    window.parent.document.getElementById('seekerBase').style.backgroundImage = 'url(' + bgImgUrl + ')';
  }
</script>

<button type="button" onclick="myFunction('http://www.floom.com/images/waveform_lotos.gif')">Get the background image of div</button>

Hope it helps!

Sign up to request clarification or add additional context in comments.

2 Comments

@Jürgen You're welcome! Please accept as Answer if it really helped. Thanks!
Sorry, I am new on this, I did it now ;)

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.