View Full Version : onClick copy image url into input field
bokanegro
03-22-2011, 10:59 PM
Hello,
Like I said in thread title, need to copy image url onClick using JavaScript and after copying link will be showed in input field.
Copy from:
<ul id="image_list">
<li><img src="dir/dir/image1.jpg" /></li>
<li><img src="dir/dir/image2.jpg" /></li>
<li><img src="dir/dir/image3.jpg" /></li>
</ul>
Copy to:
<form>
<label for="copy_img">Image</label>
<input type="text" name="copy_img" id="copy_img" />
</form>
Any help is appreciated. Thanks for your time
bluewalrus
03-23-2011, 01:28 AM
You can try this, the address is absolute though.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function clicked(address) {
document.getElementById('copy_img').value = address.src;
}
</script>
</head>
<body>
<form>
<label for="copy_img">Image</label>
<input type="text" name="copy_img" id="copy_img" />
</form>
<ul id="image_list">
<li><img src="dir/dir/image1.jpg" onclick="clicked(this)" /></li>
<li><img src="dir/dir/image2.jpg" onclick="clicked(this)" /></li>
<li><img src="dir/dir/image3.jpg" onclick="clicked(this)" /></li>
</ul>
</body>
</html>
jscheuer1
03-23-2011, 05:04 AM
You can get the exact path as specified by the src attribute by using getAttribute():
function clicked(address) {
document.getElementById('copy_img').value = address.getAttribute('src', 0);
}
Or, if you want just the filename.ext, you can use a regular expression:
function clicked(address) {
if((address = clicked.re.exec(address.src))){
document.getElementById('copy_img').value = address[0];
}
}
clicked.re = /[^\/]+$/;
bokanegro
03-23-2011, 07:11 AM
Thanks, it's working
mcflai
05-29-2013, 12:53 AM
the code works great, but how i do if i want to copy more than one image to multiple fields?
eg:
copy one image go to field 1
copy other image go to field 2
copy the first image againt go to field 3.
is it possible?
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.