0

I have the code bellow inside a wordpress loop, so the variable $perf and $url change every time.

<div class="link" data-performer="<? echo $perf; ?>">
    <a class="performer_rp" href="<? echo $url; ?>">My anchor</a>
</div>

This jquery function ads the variable $perf to the link, on click.

function performer_rp(){
    var perf;
    perf = $(".link").data('performer');
    $("a.performer_rp").click(function() {
       this.href += "?perf="+perf+"";
    });
};

Problem is, I get only the first value of $perf, it won't change with the loop.

So lets say the loop "loops" 5 times and I get 5 $perf values: value1, value2 ... value5.

The jquery code asigns the value1 everytime.

Why is that? Ty very much!

3 Answers 3

2

Well 'id do it this way:

function performer_rp(){
    var perf;
    $("a.performer_rp").click(function() {
       //get the value from the parent (as suggessted in the comment you can also use 
       //$(this).closest('div.link').data('performer'); if the '<a>' is not a direct child of the div
        perf = $(this).parent().data('performer');
       this.href += "?perf="+perf+"";
    });
};

otherwise you have the same perf each time (because it's not relative to the link you click), and you are getting a collection of perfs anyway.

Edit: i don't know exactly how data() works, but i think you could use:

 perf = $(this).parent().attr('data-performer');
Sign up to request clarification or add additional context in comments.

4 Comments

Hmm, ty, this is great, tho i get perf value as undefined, I'll check my code a bit
It works, thank you very much! Please tell me, google won't index this links with the added perf variable, right?:) this is why I'm doing it this way
@webmasters: Nicola's code should work, yeah, probably something somewhere else. If the a.performer_rp elements aren't really direct children of the div.link elements as you've shown, you might need to change $(this).parent().data('performer'); to $(this).closest('div.link').data('performer'); instead. But with the markup you've shown, where the links are really direct children of the divs, Nicola's code is fine.
@T.J. Crowder yes, closest might be better, i was speaking thinking about that markup, if you nest the link deeper inside the dive, use closest(). @webmasters i don't know thta much about google serach engine, sorry! :)
0

The selector

$(".link");

Is getting a collection that contains all of your links (say 5 in your example). When you do this

 perf = $(".link").data('performer');

You are getting the data value of the first element in the collection only

Comments

0

Hello I not sure if I've got your question. Why to assign values in the moment of a click?

Instead you can try this in order to assign all 'data-performer' values just after the page is loaded.

$(document).ready(function() {
    $(".link").each(function(){
        var parent = $(this),
            old_link = parent.find('a.performer_rp').attr('href');
            new_link = old_link + "?perf="+parent.data('performer');
        parent.find('a.performer_rp').attr('href', new_link)
    });
});

example at: http://jsfiddle.net/USEkF/

2 Comments

I want to be safe, not let google index my links with the perf attribute. It will mess all my blog in serps. Ty very much
Ok, but we never know whether google is going to start indexing also values assigned to link by javascript in either the moment of click or just after the page is loaded, i don't really think is a safe way, I'd sooned go for uncryption if something is really important

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.