jQuery get the image src












77















I hope when I click the button, I can get the specific img src and show the img src in the div class img-block block.



HTML



<button class="button">Click</button>
<div class="img1">
<img src="img.jpg" alt="">
</div>

<div class="img-block"></div>


CSS



.img-block{
position: absolute;
top:10%;
right:10%;
background-color: red;
width: 500px;
height: 500px;
}
.img1 img{
width: 200px;
}


JS



$('.button').click(function(){
var images = $('.img1 img').attr(src);
alert(images);
});


But now I face the problem is to get the img src.

So I use alert to make the test, the result is it alert nothing.










share|improve this question





























    77















    I hope when I click the button, I can get the specific img src and show the img src in the div class img-block block.



    HTML



    <button class="button">Click</button>
    <div class="img1">
    <img src="img.jpg" alt="">
    </div>

    <div class="img-block"></div>


    CSS



    .img-block{
    position: absolute;
    top:10%;
    right:10%;
    background-color: red;
    width: 500px;
    height: 500px;
    }
    .img1 img{
    width: 200px;
    }


    JS



    $('.button').click(function(){
    var images = $('.img1 img').attr(src);
    alert(images);
    });


    But now I face the problem is to get the img src.

    So I use alert to make the test, the result is it alert nothing.










    share|improve this question



























      77












      77








      77


      11






      I hope when I click the button, I can get the specific img src and show the img src in the div class img-block block.



      HTML



      <button class="button">Click</button>
      <div class="img1">
      <img src="img.jpg" alt="">
      </div>

      <div class="img-block"></div>


      CSS



      .img-block{
      position: absolute;
      top:10%;
      right:10%;
      background-color: red;
      width: 500px;
      height: 500px;
      }
      .img1 img{
      width: 200px;
      }


      JS



      $('.button').click(function(){
      var images = $('.img1 img').attr(src);
      alert(images);
      });


      But now I face the problem is to get the img src.

      So I use alert to make the test, the result is it alert nothing.










      share|improve this question
















      I hope when I click the button, I can get the specific img src and show the img src in the div class img-block block.



      HTML



      <button class="button">Click</button>
      <div class="img1">
      <img src="img.jpg" alt="">
      </div>

      <div class="img-block"></div>


      CSS



      .img-block{
      position: absolute;
      top:10%;
      right:10%;
      background-color: red;
      width: 500px;
      height: 500px;
      }
      .img1 img{
      width: 200px;
      }


      JS



      $('.button').click(function(){
      var images = $('.img1 img').attr(src);
      alert(images);
      });


      But now I face the problem is to get the img src.

      So I use alert to make the test, the result is it alert nothing.







      jquery






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 15 '17 at 3:47









      Cœur

      18.1k9108148




      18.1k9108148










      asked Nov 12 '13 at 18:32









      Chen-Tai HouChen-Tai Hou

      1,55511126




      1,55511126
























          5 Answers
          5






          active

          oldest

          votes


















          143














          src should be in quotes:



          $('.img1 img').attr('src');





          share|improve this answer



















          • 2





            got it!!! thanks very much!

            – Chen-Tai Hou
            Nov 12 '13 at 18:34






          • 1





            really sorry that I forgot to accept it.

            – Chen-Tai Hou
            Mar 28 '14 at 16:04



















          5














          You may find likr



          $('.class').find('tag').attr('src');





          share|improve this answer































            1














            This is what you need



             $('img').context.currentSrc





            share|improve this answer































              0














              for full url use



              $('#imageContainerId').prop('src')


              for relative image url use



              $('#imageContainerId').attr('src')





              function showImgUrl(){
              console.log('for full image url ' + $('#imageId').prop('src') );
              console.log('for relative image url ' + $('#imageId').attr('src'));
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <img id='imageId' src='images/image1.jpg' height='50px' width='50px'/>

              <input type='button' onclick='showImgUrl()' value='click to see the url of the img' />








              share|improve this answer































                -1















                TO get current clicked image source and display that image in other
                location;




                <script type="text/javascript">
                jQuery(document).ready(function($){
                $('body').on('click','img',function(){
                var imgsrc=$(this).attr('src');
                $("html").append("<div id='image_popup'><img src='"+imgsrc+"'></div>");
                })
                });
                </script>





                share|improve this answer























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19937162%2fjquery-get-the-image-src%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  143














                  src should be in quotes:



                  $('.img1 img').attr('src');





                  share|improve this answer



















                  • 2





                    got it!!! thanks very much!

                    – Chen-Tai Hou
                    Nov 12 '13 at 18:34






                  • 1





                    really sorry that I forgot to accept it.

                    – Chen-Tai Hou
                    Mar 28 '14 at 16:04
















                  143














                  src should be in quotes:



                  $('.img1 img').attr('src');





                  share|improve this answer



















                  • 2





                    got it!!! thanks very much!

                    – Chen-Tai Hou
                    Nov 12 '13 at 18:34






                  • 1





                    really sorry that I forgot to accept it.

                    – Chen-Tai Hou
                    Mar 28 '14 at 16:04














                  143












                  143








                  143







                  src should be in quotes:



                  $('.img1 img').attr('src');





                  share|improve this answer













                  src should be in quotes:



                  $('.img1 img').attr('src');






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 '13 at 18:33









                  Stuart KershawStuart Kershaw

                  9,57942946




                  9,57942946








                  • 2





                    got it!!! thanks very much!

                    – Chen-Tai Hou
                    Nov 12 '13 at 18:34






                  • 1





                    really sorry that I forgot to accept it.

                    – Chen-Tai Hou
                    Mar 28 '14 at 16:04














                  • 2





                    got it!!! thanks very much!

                    – Chen-Tai Hou
                    Nov 12 '13 at 18:34






                  • 1





                    really sorry that I forgot to accept it.

                    – Chen-Tai Hou
                    Mar 28 '14 at 16:04








                  2




                  2





                  got it!!! thanks very much!

                  – Chen-Tai Hou
                  Nov 12 '13 at 18:34





                  got it!!! thanks very much!

                  – Chen-Tai Hou
                  Nov 12 '13 at 18:34




                  1




                  1





                  really sorry that I forgot to accept it.

                  – Chen-Tai Hou
                  Mar 28 '14 at 16:04





                  really sorry that I forgot to accept it.

                  – Chen-Tai Hou
                  Mar 28 '14 at 16:04













                  5














                  You may find likr



                  $('.class').find('tag').attr('src');





                  share|improve this answer




























                    5














                    You may find likr



                    $('.class').find('tag').attr('src');





                    share|improve this answer


























                      5












                      5








                      5







                      You may find likr



                      $('.class').find('tag').attr('src');





                      share|improve this answer













                      You may find likr



                      $('.class').find('tag').attr('src');






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Apr 27 '17 at 7:36









                      parth-BSPparth-BSP

                      9412




                      9412























                          1














                          This is what you need



                           $('img').context.currentSrc





                          share|improve this answer




























                            1














                            This is what you need



                             $('img').context.currentSrc





                            share|improve this answer


























                              1












                              1








                              1







                              This is what you need



                               $('img').context.currentSrc





                              share|improve this answer













                              This is what you need



                               $('img').context.currentSrc






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jul 8 '17 at 2:58









                              suzukosuzuko

                              111




                              111























                                  0














                                  for full url use



                                  $('#imageContainerId').prop('src')


                                  for relative image url use



                                  $('#imageContainerId').attr('src')





                                  function showImgUrl(){
                                  console.log('for full image url ' + $('#imageId').prop('src') );
                                  console.log('for relative image url ' + $('#imageId').attr('src'));
                                  }

                                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                  <img id='imageId' src='images/image1.jpg' height='50px' width='50px'/>

                                  <input type='button' onclick='showImgUrl()' value='click to see the url of the img' />








                                  share|improve this answer




























                                    0














                                    for full url use



                                    $('#imageContainerId').prop('src')


                                    for relative image url use



                                    $('#imageContainerId').attr('src')





                                    function showImgUrl(){
                                    console.log('for full image url ' + $('#imageId').prop('src') );
                                    console.log('for relative image url ' + $('#imageId').attr('src'));
                                    }

                                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                    <img id='imageId' src='images/image1.jpg' height='50px' width='50px'/>

                                    <input type='button' onclick='showImgUrl()' value='click to see the url of the img' />








                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      for full url use



                                      $('#imageContainerId').prop('src')


                                      for relative image url use



                                      $('#imageContainerId').attr('src')





                                      function showImgUrl(){
                                      console.log('for full image url ' + $('#imageId').prop('src') );
                                      console.log('for relative image url ' + $('#imageId').attr('src'));
                                      }

                                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                      <img id='imageId' src='images/image1.jpg' height='50px' width='50px'/>

                                      <input type='button' onclick='showImgUrl()' value='click to see the url of the img' />








                                      share|improve this answer













                                      for full url use



                                      $('#imageContainerId').prop('src')


                                      for relative image url use



                                      $('#imageContainerId').attr('src')





                                      function showImgUrl(){
                                      console.log('for full image url ' + $('#imageId').prop('src') );
                                      console.log('for relative image url ' + $('#imageId').attr('src'));
                                      }

                                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                      <img id='imageId' src='images/image1.jpg' height='50px' width='50px'/>

                                      <input type='button' onclick='showImgUrl()' value='click to see the url of the img' />








                                      function showImgUrl(){
                                      console.log('for full image url ' + $('#imageId').prop('src') );
                                      console.log('for relative image url ' + $('#imageId').attr('src'));
                                      }

                                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                      <img id='imageId' src='images/image1.jpg' height='50px' width='50px'/>

                                      <input type='button' onclick='showImgUrl()' value='click to see the url of the img' />





                                      function showImgUrl(){
                                      console.log('for full image url ' + $('#imageId').prop('src') );
                                      console.log('for relative image url ' + $('#imageId').attr('src'));
                                      }

                                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                      <img id='imageId' src='images/image1.jpg' height='50px' width='50px'/>

                                      <input type='button' onclick='showImgUrl()' value='click to see the url of the img' />






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 21 '18 at 12:05









                                      NuOne T AttygalleNuOne T Attygalle

                                      821613




                                      821613























                                          -1















                                          TO get current clicked image source and display that image in other
                                          location;




                                          <script type="text/javascript">
                                          jQuery(document).ready(function($){
                                          $('body').on('click','img',function(){
                                          var imgsrc=$(this).attr('src');
                                          $("html").append("<div id='image_popup'><img src='"+imgsrc+"'></div>");
                                          })
                                          });
                                          </script>





                                          share|improve this answer




























                                            -1















                                            TO get current clicked image source and display that image in other
                                            location;




                                            <script type="text/javascript">
                                            jQuery(document).ready(function($){
                                            $('body').on('click','img',function(){
                                            var imgsrc=$(this).attr('src');
                                            $("html").append("<div id='image_popup'><img src='"+imgsrc+"'></div>");
                                            })
                                            });
                                            </script>





                                            share|improve this answer


























                                              -1












                                              -1








                                              -1








                                              TO get current clicked image source and display that image in other
                                              location;




                                              <script type="text/javascript">
                                              jQuery(document).ready(function($){
                                              $('body').on('click','img',function(){
                                              var imgsrc=$(this).attr('src');
                                              $("html").append("<div id='image_popup'><img src='"+imgsrc+"'></div>");
                                              })
                                              });
                                              </script>





                                              share|improve this answer














                                              TO get current clicked image source and display that image in other
                                              location;




                                              <script type="text/javascript">
                                              jQuery(document).ready(function($){
                                              $('body').on('click','img',function(){
                                              var imgsrc=$(this).attr('src');
                                              $("html").append("<div id='image_popup'><img src='"+imgsrc+"'></div>");
                                              })
                                              });
                                              </script>






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Jul 5 '16 at 10:03









                                              Samir KarmacharyaSamir Karmacharya

                                              652419




                                              652419






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19937162%2fjquery-get-the-image-src%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  MongoDB - Not Authorized To Execute Command

                                                  How to fix TextFormField cause rebuild widget in Flutter

                                                  Npm cannot find a required file even through it is in the searched directory