0

Is there a way to remove from img scr extension using jQuery?

meaning from this:

<img src="images/6208606.jpg" width="120" height="120" />

to this:

<img src="images/6208606" width="120" height="120" />

Thanks for help

Dom
  • 3,126
  • 14
  • 46
  • 68

3 Answers3

3

You could do:

$('img').each(function(){
    $(this).attr('src', $(this).attr('src').replace(/\.jpg/, ''));
});

If you have multiple extensions you need to look for you could do:

var exts = ['.jpg', '.gif', '.png'];
$('img').each(function(){
    var $t = $(this);
    $.each(exts, function(i,v){
        $t.attr('src', $t.attr('src').replace(v, ''));
    });
});
Jojo
  • 348
  • 1
  • 8
2

you need to provide some identification (like id, name, alt) or specific class to select image using jquery selector.

//using css class 'special' applied to images whose
//src we need to replace
var i=$('img.special');
var s = $(i).attr("src");
s = s.substring(0, s.lastIndexOf("."));
$(i).attr("src",s);
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
1
  1. Find the image using jQuery
  2. Get it's "src" property
  3. Modify it as a simple string
  4. Assign it back to "src"
artemb
  • 9,251
  • 9
  • 48
  • 68