{"id":1315,"date":"2012-10-07T04:01:43","date_gmt":"2012-10-07T04:01:43","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=1315"},"modified":"2012-10-07T04:01:43","modified_gmt":"2012-10-07T04:01:43","slug":"jquery-get-image-src-get-image-source-on-click-using-jquery-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-get-image-src-get-image-source-on-click-using-jquery-examples\/","title":{"rendered":"jQuery Get Image SRC On Click Examples"},"content":{"rendered":"<p>While working with Web Design\/Web Development &amp; jQuery, it might be desirable to get image source using jQuery. Very often, the situation can demand us to get image source on click of a button or a div, etc. so that you can do other stuff, like store the path to that image in the database, etc. So in this article, I am going to share a very easy way to <em><strong>get image source using jQuery<\/strong><\/em>. Read on to find out more.<!--more--><\/p>\n<h2>How to use jQuery to Get Image SRC (source) Value<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=1315\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>For the sake of example, let&#8217;s assume that we have an image with class &#8220;.my_img&#8221;. We need to <strong>get source of image value on click<\/strong>. So let&#8217;s place a button. Now let&#8217;s see how to get the image source value using different available methods in jQuery.<\/p>\n<h3>Here is the HTML source code for the following examples:<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Examples to Get Image Source On Click using jQuery&lt;\/title&gt;\n&lt;script type=\"text\/javascript\" src=\"js\/jquery_1.7.1_min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n\n&lt;img src=\"images\/money.jpg\" width=\"350\" height=\"350\" class=\"my_img\"&gt;\n\n&lt;br&gt;\n&lt;input type=\"submit\" name=\"sbt_get_img_src\" id=\"sbt_get_img_src\" value=\"Get Image Source\"&gt;&lt;br&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<h3>Example 1: Get Image Source using attr() method when button is clicked using jQuery<\/h3>\n<pre class=\"lang:js decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\n$('#sbt_get_img_src').click(function() {\n\nalert( $('.my_img').attr('src') );\n\n});\n\n&lt;\/script&gt;<\/pre>\n<h3>Example 2: Get Image Source using prop() method when button is clicked using jQuery<\/h3>\n<pre class=\"lang:js decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\n$('#sbt_get_img_src').click(function() {\n\nalert( $('.my_img').prop('src') );\n\n});\n\n&lt;\/script&gt;<\/pre>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=1315\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>From both the above examples, you can get the image source. But then, you must be wondering which one to use. Well, here are the cases:<\/p>\n<p>1. Use attr() if you\u00a0\u00a0using jQuery &lt; 1.6<\/p>\n<p>2.\u00a0Use prop() if you are using jQuery &gt; 1.6<\/p>\n<p>3. If you want to get only the relative path of the image (i.e. without the http:\/\/www.mydomain.com) part, use attr(). So in the Example 1, if you look at the alert when attr() is used, you will see the alert: <strong>images\/money.jpg<\/strong><\/p>\n<p>4. If you want to get the complete absolute path of the image (i.e. with the http:\/\/www.mydomain.com) part, use prop().\u00a0So in the Example 2,\u00a0\u00a0if you look at the alert when prop() is used, you will see the alert: <strong>https:\/\/theextremewebdesigns.com\/blog\/images\/money.jpg<\/strong><\/p>\n<h5>That&#8217;s it for now!<\/h5>\n<p>Do you know of any other ways to <em><strong>get image source using jQuery<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working with Web Design\/Web Development &amp; jQuery, it might be desirable to get image source using jQuery. Very often, the situation can demand us to get image source on click of a button or a div, etc. so that you can do other stuff, like store the path to that image in the database, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":395,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,13],"tags":[40,42,41],"class_list":["post-1315","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-2","tag-jquery-demos-2","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-ld","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=1315"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1315\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=1315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=1315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=1315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}