html - Change background with javascript function -


i trying make button changes background of div. want give 2 parameters javascript function.

the id of div wich change background number of background types.

for example...

    <div id="background" class="background1">      <p>choose background</p>      <a href="javascript:change(background,10)" class="btn"></a>     </div> 

the function want changes class of background div background1, background2, background3, background4... until 10 (the paramter)

i want function parameters because use in lot of divs! can´t =(

try this...

var nextid = 1; var highestid = 10; //highest image id  function change(divid) {      //change background image only...     document.getelementbyid(divid).style.backgroundimage = '../' + nextid + '.jpg';      /*or set class instead, swap above line 1 below...*/     //document.getelementbyid(divid).classname = 'backgroundclass' + nextid;      if(nextid > highestid) {nextid = 1;}     return false; } 

sample usage:

<div id="something"></div> <a href="#" onclick="return change('something')">change background</a> 

working jsfiddle here (given don't have background images, i'm alerting set to)


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -