<div>
<p><input class="check" id="check-option-1" name="name" type="checkbox">
<label for="check-option-1"></label>
</p>
<img src="http://lorempixel.com/30/30/city">
<p class="copy">1</p>
<p class="copy-2">1.1</p>
</div>
<div>
<p><input class="check" id="check-option-2" name="name" type="checkbox">
<label for="check-option-2"></label>
</p>
<img src="http://lorempixel.com/30/30/technics">
<p class="copy">2</p>
<p class="copy-2">2.1</p>
</div>$('body').on('change', '.check', function(){
if ($(this).prop('checked')) {
let img = $('img').attr('src');
}
});let img = $('img').attr('src') - вот так у меня выбирается только одна картинка.
const containerSelector = 'div';
const eventType = 'change';
const triggerSelector = '.check:checked';
const itemSelector = 'img';
const attrName = 'src';$('body').on(eventType, triggerSelector, function() {
const value = $(this)
.closest(containerSelector)
.find(itemSelector)
.attr(attrName);
});document.body.addEventListener(eventType, ({ target: t }) => {
if (t.matches(triggerSelector)) {
const value = t
.closest(containerSelector)
.querySelector(itemSelector)
.getAttribute(attrName);
}
});
<img src="http://lorempixel.com/30/30/city" data-input="check-option-1">$('body').on('change', '.check', function(){
let id = $(this).attr('id');
if ($(this).prop('checked')) {
let img = $('img[data-id="'+id+'"]').attr('src');
}
});