Try popping some alerts in there to see where the code goes or doesn't. Maybe include the the values you are using to check they are what you expect.
I would suggest using alt atrributes rather than the src attributes to identify which picture is there. You should use ALT anyway on images and you can then do a simple "IF alt tag equals 'pic1'" rather than just looking for the number 1 or 2 somewhere in the name.
I was going with easy to understand Javascript which was the minimum to achieve what was needed and therefore might help learning. Yours is obviously a better solution (although perhaps not what was wanted) but a little harder to understand for a beginner.
Last edited by DaiLaughing; 07-17-2010 at 10:50 AM.
I was going with easy to understand Javascript which was the minimum to achieve what was needed and therefore might help learning. Yours is obviously a better solution (although perhaps not what was wanted) but a little harder to understand for a beginner.
No problem ... let me know what part you don't understand (if still interested).
[CODE]<script>
setInterval(function() {SwitchPic()}, 4000);
function SwitchPic() {
var picture = document.getElementById("imgswitch");
if (picture.src.indexOf('1')) {
picture.src = "IMG/pic2.jpg";
}
Your tests are incorrect and will only evaluate false if the digit is the first character in the filename.
If a character is not found, indexOf returns -1, which evaluates true.
Bookmarks