Cinner Posted September 5, 2007 Share Posted September 5, 2007 I downloaded this simple Javascript that changes the background of an element when you press a button. Like this: <script type="text/javascript"> function ChangeBackground(background) { switch (background) { case 1: document.getElementById('myTableCell').style.backgroundImage = 'URL(/Images/Demos/ChangeBackground/Background1.gif)'; break; case 2: document.getElementById('myTableCell').style.backgroundImage = 'URL(/Images/Demos/ChangeBackground/Background2.gif)'; break; } } </script> Then you give the button this simple property: onclick="ChangeBackground(1)" or "ChangeBackground(2)". But the thing is, I have a list of items that come from a mysql datase with php, and each item has it's own two background images a user can choose from. Those images are stored as URL's in the database. Is there a way I can pass those URL's through the buttons along with the "1" and "2"? Something like ChangeBackground(1,url1). My javascript knowledge is very limited I'm afraid. Quote Link to comment Share on other sites More sharing options...
php_tom Posted September 5, 2007 Share Posted September 5, 2007 <script> var urls = Array(); <?php $res = mysql_query("SELECT * FROM bgimages"); $c = 0; while($row = mysql_fetch_assoc($res)) { echo "urls[$c] = ".$row['url']; $c++; } echo "function ChangeBackground(background) {"; echo "document.getElementById('myTableCell').style.backgroundImage = urls[$i];"; echo "}"; ?> </script> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.