0

I was wondering if anyone knew a way I would be able to find and replace all prefixes for the src of certain image files, e.g. on button click -> find all img src="Images/g/....." and replace with img src="Images/r/....."

<table>
<tr><td>
<img src="Images/jpg/g/tblLogo/fcs6.jpg" height="30" width="30">
</td><td>
<img src="Images/jpg/g/tblLogo/dcs6.jpg" height="30" width="30">
</td></tr>
<table>

As you can see from the above extract, my files are saved within a folder named 'g', I need a function that will scan my entire HTML files and change all those src's to point to the same file but within "Images/jpg/r/....."

P.S. Simplicity is the key.

MCMXCII
  • 1,043
  • 4
  • 13
  • 26
  • If _"simplicity is the key"_, why not permanently change the source, rather than dynamically change the DOM on every page load? – Sparky Oct 02 '14 at 15:44
  • 2
    And what is JavaScript going to do? It can find it, it will not save the result to the server. Better off opening up an editor, doing a find and replace, and saving. – epascarello Oct 02 '14 at 15:44
  • I need it to be able to change dynamically. I'm going to be using this to change the image files on click of a background colour changer and I need the images to be able to change as well. – MCMXCII Oct 02 '14 at 15:46
  • You were automatically shown lots of similar questions while composing your question. Please follow the guidelines by doing a search before posting a new question. Thanks. – Sparky Oct 02 '14 at 15:51

1 Answers1

0

While I question the use case as such, this would be a way to go about it:

$('img[src^="Images/jpg/g/"]').each(function() {
    this.src = this.src.replace(/^Images\/jpg\/g\//, 'Images/jpg/r/');
});
Martin Denk
  • 554
  • 3
  • 14