Reputation: 5800
The following code is working fine but I need the attr("src")
of the closest image I tried in the following way and it didn't work yet.
$(".delimg").click(function () {
var $button = $(this);
var myString = $button.closest('.swipe-image').find('img').attr("src");
var avoid ="http://20percents.com/backend/uploads/";
myString = myString.replace(avoid,'');
$("#getnew").val(myString);
});
And the following way is working but without closest, picking the first value
$(".delimg").click(function () {
var myString = $('.swipe-image').attr("src");
var avoid ="http://20percents.com/backend/uploads/";
myString = myString.replace(avoid,'');
$("#getnew").val(myString);
});
How to fix the closest issue?
HTML:
Fiddle: http://jsfiddle.net/e27r8/603/
Update1: How about with onchange and trigger
$(".delimg").on('change', function () {
var myString = $('.swipe-image').attr("src");
var avoid ="http://20percents.com/backend/uploads/";
myString = myString.replace(avoid,'');
$("#getnew").val(myString).join());
}).trigger('change');
Upvotes: 0
Views: 711
Reputation: 12300
An alternative to Arun's solution could be:
$(".delimg").click(function () {
var myString = $(this).parent().prev().attr("src");
var avoid ="http://20percents.com/backend/uploads/";
myString = myString.replace(avoid,'');
$("#getnew").val(myString);
});
Upvotes: 0
Reputation: 388316
.closest() will find an matching ancestor element, in your case the img
is not a ancestor of the button
.
You can use .closest() to find a common parent then use find() to fetch the target element
var myString = $button.closest('.swiper-slide').find('img').attr("src");
Another way is to use the logic that the img
is the previous sibling of the button
s parent so
var myString = $button.parent().prev('img').attr("src");
Upvotes: 2