Wrap a block in a link that is located in this block
There are many blocks of this design:
<div class="thumbnail boxed-content">
<div class="col-sm-12 col-md-12"></div>
<div class="col-sm-12 col-md-12">
<h3 class="item-title text-danger center-block">
<a title="DP2K-6E" href="/some.html">asdasd</a>
</h3>
</div>
</div>
How to wrap each one in a link and put the href attribute such that it is located inside this block. I did so, but something went wrong:
$('.thumbnail.boxed-content').wrap("<a href=$('this .col-sm-12:nth-child(2) h3 a').href></a>");
0
2 answers
var $blocks = $('.thumbnail.boxed-content');
var $hrefs = $('.thumbnail.boxed-content a');
$blocks.wrap(function(){
var $element = $(this);
var href = $element.find('a').attr('href');
return "<a href="+href+"></a>"
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="thumbnail boxed-content">
<div class="col-sm-12 col-md-12"></div>
<div class="col-sm-12 col-md-12">
<h3 class="item-title text-danger center-block">
<a title="DP2K-6E" href="/some1.html">first block</a>
</h3>
</div>
</div>
<div class="thumbnail boxed-content">
<div class="col-sm-12 col-md-12"></div>
<div class="col-sm-12 col-md-12">
<h3 class="item-title text-danger center-block">
<a title="DP2K-6E" href="/some2.html">second block</a>
</h3>
</div>
</div>
1
Author: EVG, 2018-06-05 11:40:24
Here, try using this code!!!
const href = $('h3.item-title>a').attr("href");
$('.thumbnail,.boxed-content').wrap('<a href="'+href+'"></a>');
0
Author: Ark_yt, 2018-06-05 10:18:29