user472285
user472285

Reputation: 2674

jQuery each function IE7

I have some links in a page that i'd like to change from

<span id="lireArticle">
<a href="/Lists/ListeActualitesCarrousel/4_.000/DispForm.aspx?ID=4" class="action">Lire l'article</a>
</span>

<span id="lireArticle">
<a href="/Lists/ListeActualitesCarrousel/2_.000/DispForm.aspx?ID=4" class="action">Lire l'article</a>
</span>

<span id="lireArticle">
<a href="/Lists/ListeActualitesCarrousel/3_.000/DispForm.aspx?ID=4" class="action">Lire l'article</a>
</span>

to

  <span id="lireArticle">
    <a href="/Lists/ListeActualitesCarrousel/DispForm.aspx?ID=4" class="action">Lire l'article</a>
    </span>

    <span id="lireArticle">
    <a href="/Lists/ListeActualitesCarrousel/DispForm.aspx?ID=4" class="action">Lire l'article</a>
    </span>

    <span id="lireArticle">
    <a href="/Lists/ListeActualitesCarrousel/DispForm.aspx?ID=4" class="action">Lire l'article</a>
    </span>

This works in Firefox, IE9, IE8 but not IE7!

it just changes the first link in IE7

jQuery("#lireArticle a").each(function(){
        jQuery(this).attr('href',jQuery(this).attr('href').replace(/\/(\d)_.(\d{3})\//,'/'));
})

How can i get it to work in IE7?

Upvotes: 1

Views: 1141

Answers (3)

Terry
Terry

Reputation: 14219

Oops made syntax error on previous post, updated. If you don't want to change the links by adding a class just use a different selector.

$('a[href^="/Lists/ListeActualitesCarrousel"]').each(function(){
     var newURL = $(this).attr('href').replace(/\/(\d)_.(\d{3})\//,'/');
     $(this).href(newURL);
});

Upvotes: -1

James Hill
James Hill

Reputation: 61812

The issue is you're using the same ID multiple times on the page. The ID of the element should be unique. Your code is finding the first instance of an element with the ID of "lireArticle" and changing all its a's. I suggest using class="" instead.

Markup:

<span class="lireArticle">
    <a href="/Lists/ListeActualitesCarrousel/4_.000/DispForm.aspx?ID=4" class="action">Lire l'article</a>
</span>

<span class="lireArticle">
    <a href="/Lists/ListeActualitesCarrousel/2_.000/DispForm.aspx?ID=4" class="action">Lire l'article</a>
</span>

<span class="lireArticle">
    <a href="/Lists/ListeActualitesCarrousel/3_.000/DispForm.aspx?ID=4" class="action">Lire l'article</a>
</span>

JavaScript:

jQuery(".lireArticle a").each(function(){
        jQuery(this).attr('href',jQuery(this).attr('href').replace(/\/(\d)_.(\d{3})\//,'/'));
})

Upvotes: 0

SLaks
SLaks

Reputation: 887459

You cannot have multiple elements with the same ID.

Use class="" instead.

Upvotes: 2

Related Questions