|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi everyone -
I have been trying to change the background images in td cells using javascipt but with no success so far. Is this actually possible? Any help or knowledge on this would be appreciated. Best wishes - Philbert javascript:smilie(' ')One example of the many bits of code I have tried is: <script language="JavaScript"> <!-- function changeBGImage(whichImage){ if (document.thanks){ document.thanks.background = 'images/' + whichImage +'.gif'; } } //--> </script> <table> <tr> <td name="thanks" background="images/blue.gif"><h1>Hello</h1></td> <td><p>Let's try <a href="#" onclick="changeBGImage('red'); return false">changing the cell background image</a></p></td> </tr> </table> |
|
#2
|
|||
|
|||
|
Use Stylesheets. aka CSS
example Code:
<html>
<head>
<style>
#Good {
background-repeat: no-repeat;
background-position: center center;
background-image: url(images/image40.gif);
}
</style>
<script>
<!--
function changeImage() {
newImage = "url(images/image41.gif)";
document.getElementById('Good').style.backgroundIm age = newImage;
}
//-->
</script>
</head>
<body>
<table>
<tr>
<td id="Good"><br>Hopefully there is a nice picture behind me :P</td>
</tr>
<tr>
<td><button onClick="changeImage()">Click Me</button><td>
</table>
</body>
</html>
Working Example Here Hope this helps you out. |
|
#3
|
|||
|
|||
|
Thanks very much nigorr
much appreciated
|
|
#4
|
|||
|
|||
|
I tried to take this a step further.
I wanted to modify the javascript part of the CSS version so I could have multiple images but it keeps returning an invalid arguement on... document.getElementById('change').style.background Image = newImage; Any help? The javascript looks like this... <script><!-- var theImages = new Array() theImages[0] = 'url(portrait1.jpg)' theImages[1] = 'url(portrait2.jpg)' function changeImage(changer) { newImage = theImages[changer]; document.getElementById('change').style.background Image = newImage; } //--> </script> Links look like... <a href="javascript:changeImage(0);">bg1</a> <a href="javascript:changeImage(1);">bg2</a> Sidenote, I did change the td id to "change" |
|
#5
|
|||
|
|||
|
Did you get the multiple images one to work? I've also been trying with no luck!
![]() |
|
#6
|
|||
|
|||
|
Quote:
try this... function pickRandom(range) { if (Math.random) return Math.round(Math.random() * (range-1)); else { var now = new Date(); return (now.getTime() / 1000) % range; } } function loadImage(){ var imgs = new Array ( "1.jpg", "2.jpg", "3.jpg" ); var ic = imgs.length; // Number of alternative images var choice = pickRandom(ic); newImage = "url(../images/home/" + imgs[choice] + ")"; if (document.getElementById) { document.getElementById('main_pict').style.backgro undImage = newImage; } else if (document.all) { document.all('main_pict').style.backgroundImage = newImage; } else if (document.layers) { document.layers('main_pict').style.backgroundImage = newImage; } } onLoad="loadImage(); |
|
#7
|
|||
|
|||
|
Setting Background Image Properly
Hello,
I think your problem is with how your setting the background Image. Stupidly, you have to include the url( ) part like css. Try doing this. blar.style.backgroundImage="url('"+your_image+"')"; Hope that works ![]() |
|
#8
|
|||
|
|||
|
Quote:
I have 1 problem with this, and that the "Click Me" thing. Can you do this without having to click on a link? I'm working on a website for my brothers company, and he wants a small image in the bottom right corner. And he want's it to change when you go to another section of the website. This is pretty simple, just put a different pic in the cells of each page. But to spice it up I'm trying to get it to randomize a picture on every load. I tried the code you wrote and it worked, buu i want it to change when you load the page. I'm terrible at coding advanced stuff, HTML and a smaller amount of PHP is OK, but JS is out of my league. If anyone has any tips on how this could be done, please reply =) Thanks. Vikholm |
|
#9
|
|||
|
|||
|
Change td background image upon loading
Hi,
Here's what I did to have the page generate a random image for the "td" background using the code found in this thread... Code:
<style type="text/css">
.main_body{
background-repeat:no-repeat;
background-image:url(default_image.jpg);
// The above background image should be a plain image the same color as your page background
}
</style>
<script type="text/javascript">
function LoadImage(){
var images=new Array();
images[0]=["image_1.jpg"];
images[1]=["image_2.jpg"];
images[2]=["image_3.jpg"];
// Following code generates a random number
var now = new Date();
var ry = now.getSeconds() % images.length;
newImage = images[ry][0];
document.getElementById('main_body').style.backgro undImage = "url(" + newImage + ")";
}
</script>
<body onload="javascript:LoadImage();">
.
.
.
<TD width="749" height="536" class="main_body" id="main_body">
|
|
#10
|
|||
|
|||
|
I am trying to change as well with no luck
the page sourche is here:
-------------------------------------- <HEAD> <style> #Good { background-repeat: no-repeat; background-position: center center; background-image: url(tamir_wed.jpg); } </style> <SCRIPT LANGUAGE="JavaScript"> // Set speed (milliseconds) var speed = 3000 // Specify the image files var Pic = new Array() // don't touch this // to add more images, just continue // the pattern, adding to the array below <?php include "dirpics.php"; ?> // do not edit anything below this line var t var j = 0 var p = Pic.length var preLoad = new Array() for (i = 0; i < p; i++){ preLoad[i] = new Image() preLoad[i].src = Pic[i] } function runBGSlideShow(){ if (document.getElementById) { document.getElementById('Good').style.backgroundIm age = Pic[j]; } else if (document.all) { document.all('Good').style.backgroundImage = Pic[j]; } else if (document.layers) { document.layers('Good').style.backgroundImage = Pic[j]; } document.getElementById('picn').innerHTML = j; document.getElementById('picln').innerHTML = p; j = j + 1 if (j > (p-1)) j=0 t = setTimeout('runBGSlideShow()', speed) } // End --> </script> </HEAD> <body dir="rtl" onload="javascript:runBGSlideShow()"> <center> <font size="5"><b>OUR WEDDING March 2009</b></font><br> <p>PICTURE No.<b id='picn'>0</b> form <b id='picln'>0</p> <br> <font size=5> <table border=1 width="500"> <tr><td id="Good">hello<br><br><br><br><br></td></tr> </table> </font> </center> -------------------------------- the page is running on: ht tp weddin g.tamir.co.il/in dex3.php why dosent it work for me ????? :-( Doron |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > changing td background images using javascript |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|