The code that you show is correct and workable. There must be some other problem, either with other javascript code you're not showing, or with HTML markup or css style. One thing though, you cannot get elements before they're parsed by the browser. The code in your post doesn't show whether or not the:
Code:
y = document.getElementById('test').getElementsByTagName('img');
executes before or after the markup in the test division has been parsed. I see you're using jQuery, so you needn't even use getElement(s) anything, you may use jQuery selectors. Also, using display none may interrupt and/or prevent the natural loading of the images by the browser, such as they might not be available when needed.
Without really knowing the overall design of the page, or the ultimate goal of the script, I'd suggest something like so, at least for starting out with just the first image in the test division showing.:
Code:
jQuery(function($){
$('#test img').css({position: 'absolute', visibility: 'hidden'}).eq(0).css({visibility: 'visible'});
});
Here's one possible scenario for a slideshow of sorts:
Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">
jQuery(function($){
var $images = $('#test img'), count = 0, tot = $images.size(), howLong = 3000;
$images.css({position: 'absolute', visibility: 'hidden'}).eq(0).css({visibility: 'visible'});
function showNext(){
count = (count + 1 + tot) % tot;
$images.css({position: 'absolute', visibility: 'hidden'}).eq(count).css({visibility: 'visible'});
}
setInterval(showNext, howLong);
});
</script>
</head>
<body>
<div id="test">
<img src="test1.jpg" alt="original image" title="">
<img src="test2.jpg" alt="original image" title="">
<img src="test3.jpg" alt="original image" title="">
<img src="test4.jpg" alt="original image" title="">
</div>
</body>
</html>
If you want more help, please explain in detail the objective of the code and provide a link to your demo page.
Bookmarks