Reputation: 3
I want to make this code shorter. Like a loop or something? It would be great if anyone helps me to make the code shorter so that I can add more nth-child. like nth-child:4, 5.
$(document).ready(function(){
var src=$(".gallery-grid-item:nth-child(1) img").data("src");
$.ajax({
url:src,
xhrFields:{
responseType:'blob'
},
success:function(data){
var url=window.URL||window.webkitURL;
$(".gallery-grid-item:nth-child(1) img").attr("src",url.createObjectURL(data));
}
});
});
$(document).ready(function(){
var src=$(".gallery-grid-item:nth-child(2) img").data("src");
$.ajax({
url:src,
xhrFields:{
responseType:'blob'
},
success:function(data){
var url=window.URL||window.webkitURL;
$(".gallery-grid-item:nth-child(2) img").attr("src",url.createObjectURL(data));
}
});
});
$(document).ready(function(){
var src=$(".gallery-grid-item:nth-child(3) img").data("src");
$.ajax({
url:src,
xhrFields:{
responseType:'blob'
},
success:function(data){
var url=window.URL||window.webkitURL;
$(".gallery-grid-item:nth-child(3) img").attr("src",url.createObjectURL(data));
}
});
});
Upvotes: 0
Views: 50
Reputation: 530
<script>
$(document).ready(function(){
numberOfNthChilds = 5
for (let i = 1; i < numberOfNthChilds + 1; i++) {
var src=$(".gallery-grid-item:nth-child(" + i + ") img").data("src");
$.ajax({
url:src,
xhrFields:{
responseType:'blob'
},
success:function(data){
var url=window.URL||window.webkitURL;
$(".gallery-grid-item:nth-child(" + i + ") img").attr("src",url.createObjectURL(data));
}
});
}
});
</script>
Upvotes: 1