Uriel Shuraki
Uriel Shuraki

Reputation: 133

Replace string within html using jquery

I'm looking for a way to add an underscore to a filename (and remove "_" from sibling) so it'll show a new "clicked" image.

for example:

Beginning with "my_image1_.png" (which means it's clicked), I want when I click on "my_image2.png" to change to "my_image2_.png"

Thanks in advance!

 $('.img_icons_result').click(function(){
  $(this).html($(this).html().replace('_.png', '.png'))
   return false;
 });
<img src="img/icons/my_image1.png" class="img_icons_result" />
<img src="img/icons/my_image2.png" class="img_icons_result" />
<img src="img/icons/my_image3.png" class="img_icons_result" />
<img src="img/icons/my_image4.png" class="img_icons_result" />

Upvotes: 1

Views: 24

Answers (1)

Suresh Atta
Suresh Atta

Reputation: 121998

You shouldn't use html. You should just replace the src attr

$('.img_icons_result').click(function(){
   $(this).attr('src', $(this).attr('src').replace('_.png', '.png'));
   return false;
 });

Upvotes: 1

Related Questions