{"id":713,"date":"2010-02-15T22:00:26","date_gmt":"2010-02-15T22:00:26","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=713"},"modified":"2010-02-16T10:46:21","modified_gmt":"2010-02-16T10:46:21","slug":"alter-img-image-tag-on-click","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/02\/15\/alter-img-image-tag-on-click\/","title":{"rendered":"Alter img (image) tag &#8211; on click"},"content":{"rendered":"<p>With javascript you can do allot of fun things that a static page, in this example I am going to use the <a href=\"http:\/\/www.w3schools.com\/Dhtml\/dhtml_dom.asp\">getDocumentID<\/a> to obtain a image (<a href=\"http:\/\/www.w3schools.com\/tags\/tag_IMG.asp\">img<\/a>) HTML tag, and whilst I have this as a javascript object I can then alter the src image details.<\/p>\n<p>I have created a &#8220;a&#8221; href HTML tag that will not go anywhere, but I am using the onclick javascript method to call the javascript function which references the id name within the img HTML tag.<\/p>\n<pre lang=\"javascript\">\r\n  <a onclick=\"javascript:alterImg()\">Alter Image<\/a>\r\n  <img loading=\"lazy\" decoding=\"async\" src=\"pic1.jpg\" width=\"250\" height=\"350\" id=\"imagesrc\"\/>\r\n<\/pre>\n<p>and within the alterImg javascript function, getting the image (img HTML tag) via the ID <\/p>\n<pre lang=\"javascript\">\r\n  function alterImg()\r\n  {\r\n    \/\/ get the element img into the imgobject variable\r\n    var imgobject = document.getElementById('imagesrc');\r\n    \/\/ change the src of the image to the other image to display.\r\n    imgobject.src = pictureImage;\r\n    \/\/ change the image to the other image.\r\n    if (pictureImage == \"pic2.jpg\") \r\n      pictureImage = \"pic1.jpg\";\r\n    else\r\n      pictureImage = \"pic2.jpg\";\r\n  }\r\n<\/pre>\n<p>Here is the full code and if you save the below as changeimage.html<\/p>\n<pre lang=\"javascript\">\r\n<!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/strict.dtd\">\r\n<html>\r\n<script language=\"javascript\">\r\n  var pictureImage = \"pic2.jpg\";\r\n  function alterImg()\r\n  {\r\n    \/\/ get the element img into the imgobject variable\r\n    var imgobject = document.getElementById('imagesrc');\r\n    \/\/ change the src of the image to the other image to display.\r\n    imgobject.src = pictureImage;\r\n    \/\/ change the image to the other image.\r\n    if (pictureImage == \"pic2.jpg\") \r\n      pictureImage = \"pic1.jpg\";\r\n    else\r\n      pictureImage = \"pic2.jpg\";\r\n  }\r\n<\/script>\r\n<body onload=\"alterImagenow()\">\r\n  <a onclick=\"javascript:alterImg()\">Alter Image<\/a>\r\n  <img loading=\"lazy\" decoding=\"async\" src=\"pic1.jpg\" width=\"250\" height=\"350\" id=\"imagesrc\"\/>\r\n<\/body>\r\n<\/html>\r\n<\/pre>\n<p>The output would be this.. click on &#8220;Alter Image&#8221;.<\/p>\n<p><script language=\"javascript\">\n  var pictureImage = \"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/02\/pic2.jpg\";\n  function alterImg()\n  {\n    \/\/ get the element img into the imgobject variable\n    var imgobject = document.getElementById('imagesrc');\n    \/\/ change the src of the image to the other image to display.\n    imgobject.src = pictureImage;\n    \/\/ change the image to the other image.\n    if (pictureImage == \"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/02\/pic2.jpg\") \n      pictureImage = \"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/02\/pic1.jpg\";\n    else\n      pictureImage = \"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/02\/pic2.jpg\";\n  }\n<\/script><br \/>\n  <a onclick=\"javascript:alterImg()\">Alter Image<\/a><br \/>\n  <img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/02\/pic1.jpg\" width=\"250\" height=\"350\" id=\"imagesrc\"\/><\/p>\n<p>You can save the images by selecting the image and right clicking save as..<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With javascript you can do allot of fun things that a static page, in this example I am going to use the getDocumentID to obtain a image (img) HTML tag, and whilst I have this as a javascript object I can then alter the src image details. I have created a &#8220;a&#8221; href HTML tag &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/02\/15\/alter-img-image-tag-on-click\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Alter img (image) tag &#8211; on click<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[99],"class_list":["post-713","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-alter-image-tag"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=713"}],"version-history":[{"count":9,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/713\/revisions"}],"predecessor-version":[{"id":725,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/713\/revisions\/725"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}