Search code examples
javascriptjqueryhtmljquery-wrap

jQuery script to collect a link, wrap it around an image, and repeat for the next div


I'm looking to apply some jquery for a mobile shopping cart I'm using. Each product is wrapped in a div with the class product. In each product there is a seperate div for the thumbnail and the details. I would like to collect the first link in the div class actions and wrap it around the thumbnail image. I would then like it to repeat for the next product and so on. Thanks.

Here is some sample html showing the flow of the products :

<div class="product">
  <div class="product-thumb"><img src="/product1.jpg"></div>
  <div class="product-details">
    <p>Product1</p>
    <div class="amount">
      <span class="price" id="product_price">$1.00</span>
    </div>
    <div class="actions">
      <a href="http://madeuplinktoproduct1detailspage"><img alt="Details" src="../images/details.png"></a>
      <a href="http://madeuplinktoproduct1cartpage"><img alt="Add to Cart" src="../images/btn-add-to-cart.png"></a>
    </div>
  </div>
</div>

<div class="product">
  <div class="product-thumb"><img src="/product2.jpg"></div>
  <div class="product-details">
    <p>Product2</p>
    <div class="amount">
      <span class="price" id="product_price">$2.00</span>
    </div>
    <div class="actions">
      <a href="http://madeuplinktoproduct2detailspage"><img alt="Details" src="../images/details.png"></a>
      <a href="http://madeuplinktoproduct2cartpage"><img alt="Add to Cart" src="../images/btn-add-to-cart.png"></a>
    </div>
  </div>
</div>

I've tried code like this:

$(document).ready(function() {
      var link1 = $('.actions a:nth-child(1)').attr('href');
      $('product-thumb img').wrap('<a href="'
        $link1 '"></a>);
      });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="product">
  <div class="product-thumb"><img src="/product1.jpg"></div>
  <div class="product-details">
    <p>Product1</p>
    <div class="amount">
      <span class="price" id="product_price">$1.00</span>
    </div>
    <div class="actions">
      <a href="http://madeuplinktoproduct1detailspage"><img alt="Details" src="../images/details.png"></a>
      <a href="http://madeuplinktoproduct1cartpage"><img alt="Add to Cart" src="../images/btn-add-to-cart.png"></a>
    </div>
  </div>
</div>

<div class="product">
  <div class="product-thumb"><img src="/product2.jpg"></div>
  <div class="product-details">
    <p>Product2</p>
    <div class="amount">
      <span class="price" id="product_price">$2.00</span>
    </div>
    <div class="actions">
      <a href="http://madeuplinktoproduct2detailspage"><img alt="Details" src="../images/details.png"></a>
      <a href="http://madeuplinktoproduct2cartpage"><img alt="Add to Cart" src="../images/btn-add-to-cart.png"></a>
    </div>
  </div>
</div>


Solution

  • Here are two ways you can do it:

    $('div.product').each(function(){
        var link = $(this).find('div.actions a:first').attr('href');
        $(this).find('div.product-thumb img').wrap('<a href="'+link+'"/>')
    })
    

    jsFiddle example

    and

    $('div.product-thumb img').wrap(function(){
        return '<a href="' + $(this).parent().next().find('div.actions a:first').attr('href') + '"/>'
    })
    

    jsFiddle example