ngplayground
ngplayground

Reputation: 21617

jQuery remove part of an image source

$('.modal-left img').data('src').replace('-90x67','');

Uncaught TypeError: Cannot call method 'replace' of undefined

Using the above code I am trying to remove -90x67 from the image source specified. I'm unable to do so using it, could someone assist?

Upvotes: 0

Views: 816

Answers (4)

Ben Pretorius
Ben Pretorius

Reputation: 4319

You can manipulate your image attr as follows:

// To Change It
$('#test img').attr('src', 'new value');

// To Remove It
$('#test img').removeAttr('src');

Example: http://jsfiddle.net/AAyss/1/

Upvotes: 0

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

try

var test=$('.modal-left img').attr('src').replace('-90x67','');

$('.modal-left img').attr('src',test);

Upvotes: 0

Try .attr()

$('.modal-left img').attr('src').replace('-90x67','');


Or better .prop()

$('.modal-left img').prop('src').replace('-90x67','');


If you want to replace it use and set replaced src

.prop( propertyName, value )

$('.modal-left img').prop('src',function(_,old){
   return old.replace('-90x67','');
});

Read .data()

Upvotes: 1

Felix
Felix

Reputation: 38102

Try to use attr() here:

$('.modal-left img').attr('src').replace('-90x67','');

or prop() instead:

$('.modal-left img').prop('src').replace('-90x67','');

Your code only works if you put your image source inside data-src attribute

<img data-src="Your image src here" src="Your img src here" />

Upvotes: 0

Related Questions