1

I'm looking for a way to replace 18 links using jQuery.

What I have is this:

    <a class="PDF-link" href="http://domain/0101_xxx.pdf" target="_blank">folder 1</a>
    <a class="PDF-link" href="http://domain/0102_xxx.pdf" target="_blank">folder 2</a>
    <a class="PDF-link" href="http://domain/0201_xxx.pdf" target="_blank">folder 2</a>
    <a class="PDF-link" href="http://domain/0202_xxx.pdf" target="_blank">folder 2</a>
    14 more like this

and I started with this jQuery:

    $(document).ready(function() {
         var client  =  "<%=UserController.GetCurrentUserInfo().Username%>";
         $('.PDF-link').prop('href', $('.PDF-link').prop('href').replace('xxx', client));
    });

But that generates the same URL for each link:

    http://domain/0101_johndoe.pdf
    http://domain/0101_johndoe.pdf
    http://domain/0101_johndoe.pdf
    http://domain/0101_johndoe.pdf

and I'd like it to be like this:

    http://domain/0101_johndoe.pdf
    http://domain/0102_johndoe.pdf
    http://domain/0201_johndoe.pdf
    http://domain/0202_johndoe.pdf

Can someone help me out?

4 Answers 4

1

The logic you're using won't work because each time you select the entire group of .PDF-link elements as a whole. Instead you can provide a function to prop which will be executed for each individual element found:

var client = "<%= UserController.GetCurrentUserInfo().Username %>";
$('.PDF-link').prop('href', function(i, val) {
    return val.replace('xxx', client);
});

Example fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

I'd use each in this case:

$(document).ready(function() {
    var client  =  "<%=UserController.GetCurrentUserInfo().Username%>";

    $('.PDF-link').each(function() {
        $(this).attr('href', $(this).attr('href').replace('xxx', client));
    });
});

Comments

0

Use this.href in a each for instead of $('.PDF-link').prop('href')
Example JSFiddle

Comments

-1

try the below

$(document).ready(function() {
     var client  =  "<%=UserController.GetCurrentUserInfo().Username%>";
     $('a.PDF-link').prop('href',  client);
});

(or using attr)

$(document).ready(function() {
     var client  =  "<%=UserController.GetCurrentUserInfo().Username%>";
     $('a.PDF-link').attr('href',  client);
});

(or using javascript this should work)

$(document).ready(function() {
     var client  =  "<%=UserController.GetCurrentUserInfo().Username%>";
     $('a.PDF-link').each(function(){
        $(this)[0].setAttribute('href',  client);
     });
});

5 Comments

This will change all links for the client's name, and that's not what the OP wants.
whos OP and the one who asked question didn't said anything like there are even other elements with the same class even if there is a simple change in the jQuery selector will do the job
Using your suggestion, all 18 links would have the same thing (what's in the var client). You should read the question more carefully.
i suggest you read the question again because that's what the user want change, all the 18 links.And in the "var client" the value comes from the server
"But that generates the same URL for each link and I'd like it to be like this". Yes, he wanted to change all links, but not to the same thing.

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.