0

I can't find the problem with this code and I've been having way too much trouble with it tonight, can anyone help me out?

<html>
    <script type="text/javascript">
      var image = new Array ();
      image[0] = "header1.png";
      image[1] = "header2.png";
      image[2] = "header3.png";
      image[3] = "header4.png";
      var size = image.length
      var x = Math.floor(size*Math.random())
      var backgroundImageFile = image[x];
      var backgroundImageUrl = "url('" + backgroundImageFile + "')";
      $('#header-image').css('background-image', 'backgroundImageUrl');
      function op()
    {
    document.getElementById('header-image').innerHTML=backgroundImageUrl;
    }
    </script>
    <body onload="op();">
    <img src="backgroundImageFile">
    </body>
    </html>
4
  • 1
    use the variable directly rather than wrapping them in quotes - $('#header-image').css('background-image', backgroundImageUrl); Commented Sep 20, 2017 at 9:07
  • what is the problem? What is not working? what issues do you have? what errors do you get? what behaviour do you expect? Commented Sep 20, 2017 at 9:07
  • the problem is that nothing prints out at all, and I've tried with and without quotes, doesn't work either way. Commented Sep 20, 2017 at 9:10
  • There is no element #header-image Commented Sep 20, 2017 at 9:14

2 Answers 2

2

You may try for this:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    var image = new Array ();
      image[0] = "https://www.w3schools.com/angular/pic_angular.jpg";
      image[1] = "https://www.w3schools.com/images/colorpicker.gif";
      image[2] = "https://www.w3schools.com/angular/pic_angular.jpg";
      image[3] = "https://www.gstatic.com/images/branding/googlelogo/2x/googlelogo_dark_color_84x28dp.png";
      var size = image.length
      var x = Math.floor(size*Math.random());
      var backgroundImageFile = image[x];
$('#imageId')[0].setAttribute('src',backgroundImageFile);
});

</script>
</head>
<body>
 //displays circle with dimensions 

    image here:
  <img src="backgroundImageFile" id="imageId">

</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

1

Please find the solution: I think you are looking for same.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
    <script type="text/javascript">
      var image = new Array ();
      image[0] = "header1.png";
      image[1] = "header2.png";
      image[2] = "header3.png";
      image[3] = "header4.png";
      var size = image.length
      var x = Math.floor(size*Math.random())
      var backgroundImageFile = image[x];
      var backgroundImageUrl = "url('" + backgroundImageFile + "')";
      $('#header-image').css('background-image', 'backgroundImageUrl');
      function op()
    {
    document.getElementById('header-image').innerHTML=backgroundImageUrl;
    }
    </script>
    <body onload="op();">
    <img src="backgroundImageFile">
    <div id="header-image"></div>
    </body>
    </html>

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.