javascript - Add each image to text area on click -


i able answer original question here, had change way doing , not working. changed too:

foreach ($image_data $key => $row) {     //print_r($row);     echo '<div class="item image-link" id="img_link" onclick="myfunction()">             <img src="'.$row['s3_link'].'" class="img-responsive img-post" />             <div class="after">                                                                                      <span class="zoom">                     <i class="fa fa-check text-success"></i>                 </span>             </div>         </div>';     }    

this 1 of many different ways have tried it:

function myfunction() {             var $this = $('.image-link');             var myimg = document.getelementsbyclassname('img-post')[0];             var mysrc = '<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3"><a class="thumbnail" href="#"><img src="'+myimg.src+'" class="img-responsive" alt="image missing" /></a></div>';             if($this.hasclass('clicked')){                 $this.removeattr('style').removeclass('clicked');                          } else{                 $this.addclass('clicked');                 $('.selected-images').append(mysrc);             }      } 

the problem add class clicked images , append first image.

i should add way function can work if take out of $(document).ready(function() {});

can tell me doing wrong?

jquery class selector select elements having mentioned class irrespective of context in invoked.

it never suggested use inline function bind events in js. should use .on bind events.

using inline-event binding, must pass this argument argument function called. jquery included, getelementsbyclassname can replaced $('.classname') not in case suppose .find() element child of clicked parent.

also note should not have multiple elements having same value id attribute.

try this:

function myfunction(elem) {    var $this = $(elem);    var myimg = $this.find('img.img-post');    var mysrc = '<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3"><a class="thumbnail" href="#"><img src="' + myimg.attr('src') + '" class="img-responsive" alt="image missing" /></a></div>';    if ($this.hasclass('clicked')) {      $this.removeattr('style').removeclass('clicked');    } else {      $this.addclass('clicked');      $('.selected-images').append(mysrc);    }  }
.clicked {    background: gray;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>  <div class="item image-link" onclick="myfunction(this)">    <img src="https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" class="img-responsive img-post" />    <div class="after">      <span class="zoom">                      <i class="fa fa-check text-success"></i>                  </span>    </div>  </div>    <div class="item image-link" onclick="myfunction(this)">    <img src="http://cordis.europa.eu/docs/news/images/2011-10/20111021-3.jpg" class="img-responsive img-post" />    <div class="after">      <span class="zoom">                      <i class="fa fa-check text-success"></i>                  </span>    </div>  </div>    <div class="item image-link" onclick="myfunction(this)">    <img src="http://rs661.pbsrc.com/albums/uu332/ilovemooks/nature-1.jpg~c200" class="img-responsive img-post" />    <div class="after">      <span class="zoom">                      <i class="fa fa-check text-success"></i>                  </span>    </div>  </div>

using .on() :

function myfunction() {    var $this = $(this);    var myimg = $this.find('img.img-post');    var mysrc = '<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3"><a class="thumbnail" href="#"><img src="' + myimg.attr('src') + '" class="img-responsive" alt="image missing" /></a></div>';    if ($this.hasclass('clicked')) {      $this.removeattr('style').removeclass('clicked');    } else {      $this.addclass('clicked');      $('.selected-images').append(mysrc);    }  }    $('.image-link').on('click', myfunction);
.clicked {    background: gray;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>  <div class="item image-link">    <img src="https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" class="img-responsive img-post" />    <div class="after">      <span class="zoom">                      <i class="fa fa-check text-success"></i>                  </span>    </div>  </div>    <div class="item image-link">    <img src="http://cordis.europa.eu/docs/news/images/2011-10/20111021-3.jpg" class="img-responsive img-post" />    <div class="after">      <span class="zoom">                      <i class="fa fa-check text-success"></i>                  </span>    </div>  </div>    <div class="item image-link">    <img src="http://rs661.pbsrc.com/albums/uu332/ilovemooks/nature-1.jpg~c200" class="img-responsive img-post" />    <div class="after">      <span class="zoom">                      <i class="fa fa-check text-success"></i>                  </span>    </div>  </div>


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -