Rahul
Rahul

Reputation: 518

Remove auto size values from an image url

I want to remove the -960x330 part from the end of this image url and I tried it with with this jQuery:

jQuery("#the-team").each(function(){
    var img_url = jQuery(this).find("#the-team img").attr("src");
    var main_url = img_url.split('-960x330');
    var exact_url = main_url[0] + main_url[1]; 
    jQuery(this).find("#the-team img").attr("src" , exact_url );
});

Here's the html if anybody is unable to check the image.

<img alt="" width="960" height="330" src="http://phpyouth.com/clients/LawPoint/wp-content/uploads/2013/09/Team-lawpoint-960x330.jpg">

enter image description here

Upvotes: 0

Views: 731

Answers (1)

Zoltan Toth
Zoltan Toth

Reputation: 47667

jQuery(this).find("#the-team img") - means searching for #the-team img inside #the-team.
You need just jQuery(this).find("img"):

jQuery("#the-team").each(function(){
  var img_url = jQuery(this).find("img").attr("src");
  var main_url = img_url.split('-960x330');
  var exact_url = main_url[0] + main_url[1]; 
  jQuery(this).find("img").attr("src" , exact_url );
});

EDIT: A bit shorter:

jQuery("#the-team").each(function() {
  var img = jQuery(this).find("img");
  img.attr("src", img.attr("src").replace("-960x330", ""));
});

FYI: Not related to the issue, but worth mentioning. This line

jQuery("#the-team").each(function(){

suggests that you have more than one element on your page with the same id, which is incorrect. Element id-s should be unique. If you need them for styling, then class is the thing you want to use.

Upvotes: 2

Related Questions