Get random background-image from this div

random background image generator
random background image on refresh
css random image rotator
react random background image
random body background image css
divi random background image
how to make background image random and keep changing in html
wordpress random background image

How can I get a random image with from example: id="myid" with javascript or jQuery? Is it possible? Thanks

<div id="myid" class="myclass">    
  <span class="AvGbtn" style="background-image: url(/uploads/articles/768eec43.jpg); background-size: 100% 100%;" rel="nofollow"></span>
  <span class="AvGbtn" style="background-image: url(/uploads/articles/a79e6b78.jpg); background-size: 100% 100%;" rel="nofollow"></span>  
  <span class="AvGbtn" style="background-image: url(/uploads/articles/27570903.jpg); background-size: 100% 100%;" rel="nofollow"></span>     
</div>

I assume that you need to access the url value of the style attribute of the many spans, and want to select one of the url by random.

$(document).ready(function(){
  var spans = $('#myid span[style]');
  var arr = [];
  spans.each(function(i,d){
    var str = $(d).attr('style');
    var url = str.substring(str.indexOf('url')+4,str.indexOf(";")-1);
    arr.push(url);
  });
  
  var r = Math.floor(Math.random()*arr.length);
  
  console.log(arr[r]);
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div id="myid" class="myclass">    
  <span class="AvGbtn" style="background-image: url(/uploads/articles/768eec43.jpg); background-size: 100% 100%;" rel="nofollow"></span>
  <span class="AvGbtn" style="background-image: url(/uploads/articles/a79e6b78.jpg); background-size: 100% 100%;" rel="nofollow"></span>  
  <span class="AvGbtn" style="background-image: url(/uploads/articles/27570903.jpg); background-size: 100% 100%;" rel="nofollow"></span>     
</div>

Randomize Background Image, jpg' ); // array of filenames $i = rand(0, count($bg)-1); // generate random number size of the array $selectedBg = "$bg[$i]"; // set variable equal to which random  I'm trying to get a randomly picked background image (from a selection of 4 images) to appear as the background image for a asp.net panel. The problem I have is that the code works when stepping through the code in debug mode. Once you run the code on the website without debugging, all the images are the same.

var selector=$("#myid > span")
var imageURL=[]
for(var i=0;i<selector.length;i++)
{
var bg=$("#myid > span")[i].style["backgroundImage"];
var startIndex=bg.indexOf("(");
var EndIndex=bg.indexOf(")");
imageURL.push(bg.substring(startIndex+1,EndIndex));
}
var randomURL = imageURL[Math.floor(Math.random() * imageURL.length)];
console.log(randomURL)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="myid" class="myclass">    
  <span class="AvGbtn" style="background-image: url(/uploads/articles/768eec43.jpg); background-size: 100% 100%;" rel="nofollow"></span>
  <span class="AvGbtn" style="background-image: url(/uploads/articles/a79e6b78.jpg); background-size: 100% 100%;" rel="nofollow"></span>  
  <span class="AvGbtn" style="background-image: url(/uploads/articles/27570903.jpg); background-size: 100% 100%;" rel="nofollow"></span>     
</div>

JS random background image, To get the best cross-browser support, it is a common practice to apply vendor prefixes to <div id="random" ></div>. 3 backgroundImage=bigSize[random];. I'm currently working on a site that requires a background image inside of a div tag, that upon refreshing of the page the image changes. Originally I had this working fine, as a bg image for the

You can do it by the following way. This will select random images using jquery.

$(function () {
  var images = [
    'image-01.jpg',
    'image-02.jpg',
    'image-03.jpg'
  ];
  $('.imageClass').css(
    { 
      'background-image': 'url(Styles/Default/Images/ImageLocation/' + 
        images[Math.floor(Math.random() * images.length)] + ')' 
    }
  );
});

Random Background image inside a div tag - JavaScript, I've tried solving this multiple ways using php, javascript, and css. I'm getting desperate. Any help would be greatly appreciated! here is the link to  With a div, the background image is constrained within the div. Any divs on a page can have their own background image. Div content is on top of the div's background image like web page content is on top of its background image. Some content may benefit from an underlying background image.

get a randomly picked background image to appear as the , And when should the random background be applied to the div? Is it on load or some event? <header class="header"> <div id="logoArea"  My initial guess is to use some jQuery that runs once the page is loaded. Use the same randNum function that you have as well as an array of potential hex values/images to use to update the background image. Then use some more jQuery that will take that value from the random position of the array and update the css.

Here is a sample fiddle for you.

https://jsfiddle.net/voqfnzus/7/

Javascript

var images = [
  "https://images.pexels.com/photos/1020315/pexels-photo-1020315.jpeg?auto=compress&cs=tinysrgb&h=350", 
  "https://images.pexels.com/photos/207171/pexels-photo-207171.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
  "https://images.pexels.com/photos/719609/pexels-photo-719609.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"
]

function showNextRandomImage() {
  var nextRandomImage = images[Math.floor(Math.random() * images.length)];
  $("#AvGbtn").css("background-image", "url(" + nextRandomImage + ")");
}

$("#btn").click(function() {
  showNextRandomImage();
});

$(document).ready(function() {
  showNextRandomImage();
});

HTML

<div id="myid" class="myclass">
  <span id="AvGbtn" style="" rel="nofollow"></span>
</div>

<button id="btn">
  Change
</button>

CSS

#AvGbtn {
  background-size: 100% 100%;
  display: block;
  margin: 12px;
  width: auto;
  height: 200px;
}

jQuery, a button that when clicked, makes all the divs' background color to randomly change. The probability of getting white (FFFFFF) is so remote as to be next to​  Howdy. I see this is an old post, but I’m using WP-Super Cache for page optimization and it is preventing the random image code from working. I get the same image on every page refresh or click through to another page. When I turn off WP-Super Cache it works fine. Is there a workaround to get the random image and the caching plugin to coexist?

Random Background Image with JavaScript., Random Background Image with JavaScript. Hey Everyone! I am trying to write a JavaScript that will randomly change the background image on load. .com/​questions/14602482/random-image-background-in-div  Edit: If you do not have access to the PHP source code on your site, try this random background image script generator. Here’s something I’ve been playing with lately to add a little bit of visual dynamic interest to sites that don’t have frequently changing content.

HTML DOM Style backgroundImage Property, The backgroundImage property sets or returns the background image of an element. Example. Set a background image of a specific <div> element: document. As you only have 2 random images sometimes the same image will be selected as there is only 2 to choose from. As you can see from my example everything is working ok. Paul

How to change the background image using jQuery ?, jQuery Changing background-image CSS Property. </ title >. < style > .box {. width: 700px; Change background image of a div by clicking on the button. JavaScript exercises, practice and solution: Write a JavaScript function to create random background color.

Comments
  • Have you tried anything? You should really have a look at SO rules before posting homework ;)
  • yes, this is possible. But when you say random image, do you mean select any random image from the internet, or do you mean select a random image (ie shuffle the order) of the images you currently have?
  • to be honest, I'm new to this field, I don't know moch about script, I jus have start to leran somthing from internet
  • how I have say before, im new, so i see your code works but, I still need help how to display id on background? Thanks again
  • can you show me how I can do it, to change every page load not on button click thanks again
  • @Leo I have updated my code to run after page is loaded.
  • Thank you very mouch for your help
  • Sure thing. Just please, remember, homework is meant to be done by oneself. Even if you have no desire what so ever to become developer one day, you can still develop yourself mentally ba solving it, programming is mostly logic, order and to be honest good Google skills. All 3 are extremely beneficial in most areas of work these days. Play with it a bit, spend some time developing brain power, you will thank me later ;)
  • Thanks for your help