admin管理员组文章数量:1389897
I have a snippet that is like this:
<div id="image_input">
<img src="missing-image.png" name="image_p" />
<input type="text" name="image_url" size="37" />
</div>
I want that when someone unfocus image_url
or even do a onChange
event, a Javascript function called changeImage()
be raised(this I already know how to do, now es what I don't) and change the image_p
changes to the URL that is written in image_url
. How to do this?
I have a snippet that is like this:
<div id="image_input">
<img src="missing-image.png" name="image_p" />
<input type="text" name="image_url" size="37" />
</div>
I want that when someone unfocus image_url
or even do a onChange
event, a Javascript function called changeImage()
be raised(this I already know how to do, now es what I don't) and change the image_p
changes to the URL that is written in image_url
. How to do this?
- maybe document.getElementByName('image_p').src = document.getElementByName('image_url').value; – Zhasulan Berdibekov Commented Jan 11, 2011 at 3:31
-
2
getElementByName
is not a function, onlygetElementsByName
. This is because the name does not need to be unique likeid
.getElementsByName
returns an array of objects with the specified name. – Mark Baijens Commented Jan 11, 2011 at 3:34
3 Answers
Reset to default 4document.getElementsByName("image_p")[0].src = document.getElementsByName("image_url")[0].src;
if you have more elements with the same name you can loop trough them or use id's if you want to be sure it you do this for only 1 element.
<div id="image_input">
<img src="missing-image.png" name="image_p" id="image_p" />
<input type="text" name="image_url" size="37" id="image_url" />
</div>
You can define ids for both the elements and have this in your onChange function:
document.getElementById('image_p').src = document.getElementById('image_url').value;
<div id="image_input">
<img src="missing-image.png" id="image_p" />
<input type="text" name="image_url" id="image_url" size="37" />
</div>
<script type="text/javascript">
jQuery('#image_url').bind('change blur', function () {
jQuery('#image_p').attr('src', this.value);
});
</script>
本文标签: htmlChanging The Image Source Using JavascriptStack Overflow
版权声明:本文标题:html - Changing The Image Source Using Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744578228a2613757.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论