Le 5/29/10 5:58 AM, bruce a écrit :
Why doesn't this work? When I execute the "onClick" event, the image
appears briefly, then goes away.
because you don't avoid the submitting and the form's action fires
(here that will refresh same displayed page)
<input type="submit" onclick="return false">
or, much better :
I'm trying to develop a page where I can change the image being
displayed.
<script type='text/javascript'>
function getImage() {
var image = "images/MtRushmore.JPG" ;
document.getElementById('imgSpot').src=image;
}
</script>
<form name="form1" id="form1" method="post" action="">
<img id='imgSpot' name='imgSpot' src="" height="200" width="200"/>
tags auto-closing only if Xhtml (the '/' in end of tag)
size is optional
attribute 'alt' omitted
'name' for 'img' can be admitted
(depends of the doctype, not allowed in Xhtml not "transitional")
no '?'
<input name="Submit" type="submit" onclick="getImage()"
value="Submit" />
'name' has not to be set if you don't need to use that element in JS or
server-side.
'value' may be omitted
preferable to use the form's JS method/function 'onsubmit'
</form>
Thanks for the help
Examples :
1) (html) :
===========
<script type='text/javascript'>
function getImage(imageContainer) {
var image = "images/MtRushmore.JPG" ;
imageContainer['imgSpot'].src = image;
imageContainer.style.display = 'none';
return false;
}
</script>
<form action="" onsubmit="return getImage(this)">
<div>
<img name='imgSpot' src="" alt="the image" title="">
<br />
<input type="submit" value="Show image">
</div>
</form>
2) (xhtml) : (expecting that script is in the head)
============
<script type='text/javascript'>
function getImage(theLink) {
var image = theLink.href;
document.getElementById('imgSpot').src = image;
theLink.style.display = 'none';
return false;
}
</script>
<p>
<a href="images/MtRushmore.JPG" onclick="return getImage(this)">
show image
</a><br />
<img id="imgSpot" src="#" alt="the image" title="" />
</p>
3) (html) :
===========
<script type='text/javascript'>
function getImage(theLink) {
var image = theLink.href;
document.images['imgSpot'].src = image;
theLink.style.display = 'none';
return false;
}
</script>
<p>
<a href="images/MtRushmore.JPG" onclick="return getImage(this)">
show image
</a><br />
<img name="imgSpot" src="#" alt="the image" title="">
.... closing tag for 'p' is optional (except in Xhtml)
info (alt and title in img tag) :
=================================
The alt attribute must be set (even if it is empty)
The empty attribute 'title' avoid the bull-info with the 'alt' content
(even with IE),
that title may get another content than the alt,
the bull-info will show the title content then (even with IE).
The alt attribute is to show a text of replacement if the image's file
doesn't exists, or at a wrong address (IE is wrong showing that in a
bull-info)