пацан
пацан

Reputation: 106

Change attribute value basing on existing value with Javascript

I'll illustrate with an example: I need to convert the following html with javascript

<a href="aaa.kmz"></a>
<a href="eee.kmz"></a>
<a href="rrr.kmz"></a>
...

to code where all href values has changed only the last letter

<a href="aaa.kml"></a>
<a href="eee.kml"></a>
<a href="rrr.kml"></a>
...

Upvotes: 0

Views: 249

Answers (1)

Richard Dalton
Richard Dalton

Reputation: 35793

Get the a tags, loop through them and replace .kmz with .kml:

​var tags = document.getElementsByTagName("a");

for(var i = 0, l = tags.length; i < l; i++) {
    tags[i].href = tags[i].href.replace('.kmz', '.kml');
}​​​​

Working Example - http://jsfiddle.net/Ln4s4/

Upvotes: 4

Related Questions