Help! basic JS function problem
Posted: Wed Dec 14, 2011 8:46 pm
In the following code I have a script and then I want to run the same script as a function that is called by pressing the button. But the function doesn't work. Can you help me to understand why please?
<!DOCTYPE html>
<html>
<head><title></title></head>
<body>
<img src="klematis.jpg" width="150" height="113" />
<img src="klematis2.jpg" width="152" height="128" />
<script type="text/javascript">
function show_images(){
var imgs = document.getElementsByTagName('img');
if(imgs.length > 0){
for(var i=0; i<imgs.length; i++){
document.write("The source of image object ",(i+1));
document.write('<br />');
document.write(imgs.src);
document.write('<br />');
}
}
}
</script>
<script type="text/javascript">
var imgs = document.getElementsByTagName('img');
if(imgs.length > 0){
for(var i=0; i<imgs.length; i++){
document.write("The source of image object ",(i+1));
document.write('<br />');
document.write(imgs.src);
document.write('<br />');
}
}
</script>
<input type="button" onclick="show_images()" value="Show images" />
</body>
</html>
<!DOCTYPE html>
<html>
<head><title></title></head>
<body>
<img src="klematis.jpg" width="150" height="113" />
<img src="klematis2.jpg" width="152" height="128" />
<script type="text/javascript">
function show_images(){
var imgs = document.getElementsByTagName('img');
if(imgs.length > 0){
for(var i=0; i<imgs.length; i++){
document.write("The source of image object ",(i+1));
document.write('<br />');
document.write(imgs.src);
document.write('<br />');
}
}
}
</script>
<script type="text/javascript">
var imgs = document.getElementsByTagName('img');
if(imgs.length > 0){
for(var i=0; i<imgs.length; i++){
document.write("The source of image object ",(i+1));
document.write('<br />');
document.write(imgs.src);
document.write('<br />');
}
}
</script>
<input type="button" onclick="show_images()" value="Show images" />
</body>
</html>