Question

I am trying to build a photo gallery that pulls photos from flickr and displays each photo with a text box. I would like to have the below code, where div class='col-sm-6 col-md-4 center' and all child relations repeat within div class="row".

<div id="body" style="padding-top: 60px;">
 <div class="row">

  <!-- Section to repeat -->
  <div class='col-sm-6 col-md-4 center'>
   <div class='thumbnail'>
    <div id="gallery"></div> <!-- Photos go here -->
    <div class='input-group'>
     <!-- textbox and button -->
     <input type='text' class='form-control' placeholder='#tag'>
     <span class='input-group-btn'>
      <button class='btn btn-default' type='button'>Tag!</button>
     </span>
    </div>
   </div>
  </div>

  <!-- Above section repeats here 6 times -->

 </div><!--./row -->
</div><!--./body -->

The Javascript that I have gotten closet with is:

<script>
 (function() {
  var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
  var tagForm = "<span class='input-group-btn'><button class='btn btn-default' type='button'>Tag!</button></span>";

  $.getJSON( flickerAPI, {
   tags: "porsche",
   tagmode: "any",
   format: "json"
  })
  .done(function( data ) {
   $.each( data.items, function( i, item ) {
    $( "<img>" ).attr( "src", item.media.m ).appendTo( "#gallery" );
    $( "#gallery" ).append( tagForm );
    if ( i == 5 ) {
     return false;
    }
   });
  });
 })();
</script>

This will pull the photos, but the textbox and button will to the right of the photo rather then bellow. I know the problem is from first 2 divs not being included, however, I don't know how embed the photo and textbox/button within the thumbnail and col-sm-6 dig's.

Any help would be amazing! Thanks ahead of time.

Was it helpful?

Solution

Provided that you want each image to go into the #gallery with its own tag button it will be much easier to style if you group the image and span inside an element.

example output:

<div class="gallery">
    <div class="img-box">
        <img src="something.jpg">
        <span class='input-group-btn'><button class='btn btn-default' type='button'>Tag!</button></span>
    </div>
</div>

<script>
 (function() {

  var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
  var tagForm = "<span class='input-group-btn'><button class='btn btn-default' type='button'>Tag!</button></span>";
  // store this so we dont look it up every iteration
  var $gallery = $("#gallery");

  $.getJSON( flickerAPI, {
   tags: "porsche",
   tagmode: "any",
   format: "json"
  })
  .success(function( data ) {
   $.each( data.items, function( i, item ) {
        // create a div to stuff img and tag in
        var $img_box = $('<div class="img-box">');    
        $( "<img>" ).attr( "src", item.media.m ).appendTo( $img_box );
        $img_box.append( tagForm ).appendTo($gallery)
    if ( i == 5 ) {
     return false;
    }
   });
  });
 })();
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top