jamjam
jamjam

Reputation: 3269

Replace one attribute with another using jQuery

Using jQuery and given this

<ul>
<li>
<a external="http://stackoverflow.com" href="home.htm">Link 1</a>
</li>
<li>
<a href="about.htm">Link 2</a>
</li>
<li>
<a external="http://google.com" href="contact.html">Link 3</a>
</li>
</ul

I want to grab the links with a attribute of "external". Use the value of the external attribute to update the href.

So link 1 and 3 should end up pointing to stackoverflow.com and google.com respectively.

Upvotes: 2

Views: 2448

Answers (2)

Michal B.
Michal B.

Reputation: 5719

Put this in $(document).ready body:

$('li a[external]').each(function() {
   var ext = $(this).attr('external');
   $(this).attr('href', ext);
});

Upvotes: 1

osahyoun
osahyoun

Reputation: 5231

$('a[external]').each(function(i, el){
  $(el).attr('href', $(el).attr('external'));
});

Upvotes: 11

Related Questions