0

I have the below html code:

<td colspan="2" class="logo_padding">
   <table cellpadding="0" cellspacing="0" width="80%">
       <tbody>
          <tr>
            <td>
                <a class="logo" href="mysite.com/"><img src="http://mysite/images_mysite/logo.gif" alt="" title="" border="0"></a>
            </td>
          </tr>                     
      </tbody>
   </table>

I don't have control over the html so I need to change the img to jpg to test how it looks :

so I want to replace this string

   http://mysite/images_mysite/logo.gif with this : http://mysite/images_mysite/logo.jpg
1
  • 1
    And where are you stuck? Post your attempt Commented Apr 8, 2014 at 8:40

4 Answers 4

2

Without further information:

$('a.logo img').attr('src', function(_, src ) {
    return src.replace(/\.gif$/, '.jpg');
});
Sign up to request clarification or add additional context in comments.

Comments

2

Try to use the attribute ends with selector and the receiver function of .attr() to accomplish your task,

$('img[src$=".gif"]').attr('src',function(i,val){
  return val.substring(0,val.lastIndexOf('.')) + ".jpg";
});

DEMO

Comments

0

You should catch the image, and change its src property

$(".logo_padding a.logo img").prop("src", "http://mysite/images_mysite/logo.jpg");

Comments

0

You can do:

$('.logo img').each(function() {
    var src = $(this).attr("src");
    $(this).attr("src", src.replace(/\.gif$/i, ".jpg"));
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.