set an attribute background image javascript

document.body.style.backgroundImage = "url('img_tree.png')";

4
7
Ahmy 80 points

                                    function bg() {

   var imgCount = 3;
   var dir = 'http://local.statamic.com/_themes/img/';
   // I changed your random generator
  	//floor: helps getting a random integer
   var randomCount = (Math.floor(Math.random() * imgCount));
   // I changed your array to the literal notation. The literal notation is preferred.
   var images = ['001.png', '002.png', '003.png'];
   // I changed this section to just define the style attribute the best way I know how.
   document.getElementById('banner').setAttribute("style", "background-image: url(" + dir + images[randomCount] + ");background-repeat: no-repeat;background-size: 388px 388px");
}
// Don't forget to run the function instead of just defining it.
bg();

4 (7 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
set css background image property with javascript set body background image javascript set background image to src attribute javascript assign background image in javascript setting background image in javascript set background image in javascript javascript set background image url dom set background image background-image js style where to put background image javascript dom element set attribute background image js setattribute background image JavaScript .style.backgroundimage style background-image url javascript background image dom javascript How to set the background image through javascript javascript set image as background javascript style.backgroundimage js style.background image dom element set background image background-image at js how to set the background image property using javascript set attribute background image with javascript background image with html tag in javascript set element background image javascript set background image using js javascript setattribute style background-image html dom background image style.background image in javascript how to set background image in javascript html attribute for setting background image js style background image url set background image with javascript js style background image how to set a background image url in js javascript set element background image javascript set background image element background image js set background image dom manipulation background image style javascript javascript background image style url how to set background image javascript change the background image in javascript change image from background image to original image in javascript change style background image with javascript imagebaground js loafd bakground image from javascript javascript change background image url javascript background image css fix background image js how i put a new background image in js how to put a background in javscript set a div background image js typescript [style.background-image] how to set background image in js style set kacgground image js javascript function to change background image setproperty background image javascript javascript style background image in another background image url in javascript add background from js how to change background image url class in javascript background image using javascript javascript replace background image document.body.style.backgroundimage js in background example js image in css url set image as background javascript set background image with js how to make picture change background color javascript js style background image by id e.target.style.backgroundimage react e.target.style.backgroundimage js set backgroundimage body background image with javascript change background image css with javascript set background image in js javascript project setting background image change background image javascript css how to change background image of div js set div background image of div javascript how to make a background in javascript change background image on javascript change image background wirh javascript set div background image javascript javascript background piv how to update background image of a div with js change body background image javascript html5 change css background image for javascript change background image woth js javascript set html background image javascript change backgroung image javascript style background-image how to make background photo in js javascript set style background image js css background image background js imag .background image js add background image using javascript js change bg image change background image dom how to change backgroundImage property in css with javascript javascript to change background url on change change background image using js how to get background image in javascript js change background image css javascript add background image to div how to add background image in java script div style background image example jsdoodle set background image css through js set attribute method to add background iamge set image background javascript background image png javascript js get element background image javascript change background-image how to access background image in js css background image from js set baground imag e via js how to add a background image in javascript element setting background url in js js set attribute background image how do I set the background image of a div in js javascript css background image how to change ::after background by js create a background with js how to add a background image in javascript change div class background image document.getElementById('body').style.backgroundImage setAttribute("style","background-image: url(''); how to change the body background url in javascript background in js how to add background image in javascript javascript change bacground image of a website background bild mit javascript how to put background in js insert into background style a event field javascripy javascript dynamic background image javascript set backgroudn image javascript set background image from loaded file javascript set background image file how to set image bg in javascript set background image pure javascript js change background image add background using js changing a background url in javascript select background image js javascript change css background image javascript getelementbyid style background image background image javascript css how to change background image on javascript dom background image how to set background image js js set backround style how to set background image in js background url in js how to change the background image using javascript else if in javascript javascript background image url change the url property in css with js dom how to change background image url in css with javascript set background image url javascript how to set an image as background in js js how to set the body background style background img javascript javascript set image as background for another image backgroundImage javascript background image css in javascript set background javascript background-image javascript change background image of browser html css js how to change background image of browser using javascript how to change background image of html javascript css how to change background image in javascript element get style background imae javascript set background image js css how to add background images using javascript background image js javascript setting background image js setattribute background-image html body background image javascript vanilla js background image how to change background image using javascript javascript how to set background image javascript set body background image style background image js style background image javascript javascript change background images change backgroundimage js javascript how to cover background image to change the background image using javascript element.style.backgroundImage js change background image using javascript css background image style in dom style.backgroundimage javascript js change image based on background change image based on background javascript js backgroundimage javascript coding background images body change background image javascript javascript change body background image set background url javascript change background image with javascript how to get background image with javascript how to manipulate the backgroundimage of a div using javascript how to set bakcgorund to orignal in js set background image using javascript div style background image js js change style backgroundimage background image change javascript change css image url javascript document.getElementById('').style.bacbackgroundImage document.getelementbyid().style.backgroundimage.url document.getelementbyid().style.backgroundimage change image background with event javascript js set background image set background by javascript background image change in javascript style.backgroundImage div set background image javascript js background imagee js background image background image for website js how set bg img using js style how set bg img using js javascript access url in background how to add background image in js set background image by javascript changing background image javascript change background image js javascript style background image javascript change background image of body javascript change background image set background image javascript how to change background image with javascript css-in-js background images background images css-in-js background image url javascript javascript update background image how to givethe background image in create element div in javascript how to set the background image in javascript javascript setattribute background image how to change background image javascript js background image url how to add a background image in js style css function how to add a background image in js style function how to get background image in js chnge body background if statement javascript background image javascript js change element background image change background image body javascript javascript to change background mage how to change body background image in javascript changing css background image javascript js background url how to get the bacground image of a body in js change background image javascript how to Set a background image on the body itself with javascript set body background image js setattribute background image javascript set background image js set background image css url javascript javascript background image set attribute javascript background image set an attribute background image javascript
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source