Ragevortex
Ragevortex

Reputation: 11

jquery replace each link attr for a set of divs

Hi there This is my first post so please bear with me.

I have a large "responsive" page that loads (up to 10) slides programatically from a bunch of json data trough php(I didn't build the thing). The ressult I have to deal with is a set of divs containing links and images.

I need to change the actual href of the links to something else because the source link and the actual page I'm working on has a diferent structure.

Basically I need to take the href and compare it. If it's one thing change it for the other.

Example of what I'm working on.

Sample divisions imagine 10 of these auto generated:

<div id="slidesdiv" class="validslider"> <a href="/something/somewhere"><img src="#" width="0px" />Firstlink</a></div>
<div id="slidesdiv" class="validslider"> <a href="/somethingelse"><img src="#" width="0px" />SecondLink</a></div>

Sample code

$(document).ready(function () {
    $('div#slidesdiv a').each(function () {
        if ($(this).attr('href') !== '/something/somewhere') {
            $(this).attr('href', 'reallylongtring1');
        } else if ($(this).attr('href') !== '/somethingelse') {
            $(this).attr('href', 'reallylongstring2');
        }
    });
});

The problem I've been having is that it's not applying the right link to the right tag. It's a little wierd. Any help is welcome and I hope I've explained the issue clearly enough.

I've been working on a jsfiddle which I finally got working however it's giving me the wrong ressult. This is a jsfiddle of that example.

Upvotes: 1

Views: 69

Answers (1)

roxxypoxxy
roxxypoxxy

Reputation: 3121

Try selector $('.validslider a') in .each. You cannot have multiple elements containing same id, use class.

Upvotes: 2

Related Questions