I am getting console.log($(this).attr('src')) as undefined. I want to modify src attribute of all img
elements in loop.
$(document).ready(function() {
$("#btn1").click(() => {
$(".images").each(() => {
console.log($(this).attr('src')); // getting undefined
var srcText = $(this).attr('src');
var srcArr = srcText.split('?').split('?');
srcArr[srcArr.length - 1] = genRandQuery();
$(this).attr('src', srcArr.join('?'));
});
});
function genRandQuery() {
return 'rnd=' + Math.random().toString(36).substr(7);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<img class="images" src="https://abc?rnd=1234">
<img class="images" src="https://abc?rnd=2354">
<img class="images" src="https://abc?rnd=9876">
<button id="btn1">Show Text</button>