Reputation: 19
Is there a way to make each image (that has a certain class) clickable?
I want jQuery to:
src
of the image.a href
that contains that src url..clickable {
padding: 20px;
border: 1px solid blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="https://www.google.com/logos/doodles/2020/stay-and-play-at-home-with-popular-past-google-doodles-fischinger-2017-6753651837108768-s.png" class="clickable">
Upvotes: 0
Views: 178
Reputation: 11416
You can do it like this:
$("img.clickable").wrap("<a href='" + $('img').attr('src') + "'></a>");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="https://www.google.com/logos/doodles/2020/stay-and-play-at-home-with-popular-past-google-doodles-fischinger-2017-6753651837108768-s.png" class="clickable">
Upvotes: 1
Reputation: 29
can you brief the scenario, not getting your point?
got your point: - you can use the following to wrap all the images having class clickable with and and with href as the image src
$('.clickable').each(function(){
src = $this.attr('src');
$this.wrap("<a href='" + src + "'></a>");
})
Upvotes: 0
Reputation: 1855
$(document).ready(function(){
$("img.clickable").each(function(i){
$(this).wrap(`<a href="${($(this).attr('src'))}"></a>`)
})
})
.clickable {
padding: 20px;
border: 1px solid blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="https://www.google.com/logos/doodles/2020/stay-and-play-at-home-with-popular-past-google-doodles-fischinger-2017-6753651837108768-s.png" class="clickable">
Upvotes: 1
Reputation: 21
try this:
var url=$(".clickable").attr("src");
$("a").attr("href", url);
Upvotes: 0
Reputation: 467
I hope you want something like this:
$('.clickable').on('click', function() {....})
let me know if I am wrong.
Upvotes: 0