views:

38

answers:

5
+2  Q: 

jQuery update link

Here is html:

<a href="http://site.com/any/different/folders/picture_name.jpg"&gt;Go and win</a>
<a href="http://site.com/not/similar/links/some_other_name.png"&gt;Go and win</a>

How to add some text after last "/" in href attribute (before picture_name.jpg) of each link?

The script should give something like:

<a href="http://site.com/any/different/folders/user_picture_name.jpg"&gt;Go and win</a>
<a href="http://site.com/not/similar/links/user_some_other_name.png"&gt;Go and win</a>

Here user_ is added.

Each link is var img_link

There can be any length of the link.

+3  A: 

So I would think that the best solution would make use of splice and join.

jQuery Example

$("a").each(function(){
var arr = $(this).attr("href").split("/")
arr[arr.length-1] = "_user" + arr[arr.length-1];
$(this).attr("href",arr.join("/"));
});
runxc1 Bret Ferrier
Possible, but there are pure regex ways that are probably *a lot* faster (and shorter). This code is easier to understand, though.
MvanGeest
I find a simple regex solution easier to understand, but that's probably just me.
Senseful
href already placed in "var img_link"
Happy
+7  A: 

This solution uses regular expressions which are excellent at simple string manipulation such as this:

$('a[href]').each(function() {
  var img_link = $(this).attr('href');
  $(this).attr('href', img_link.replace(/([^\/]+)$/, "user_$1"));
});

Updated to use img_link as requested by OP.

If you already have the img_link variable and your own each() loop, simply use the one line inside the function, i.e.:

  $(this).attr('href', img_link.replace(/([^\/]+)$/, "user_$1"));
Senseful
nice, better answer than mine for sure
Shawn Simon
+1 Had almost finished writing something very similar.
MvanGeest
href already placed in "var img_link", can you add code for it?
Happy
Did you test this? Doesn't work for me at all.
patrick dw
@partick: I fixed a parenthesis, thanks.
Senseful
That looks better.
patrick dw
worked for me in regex buddy
Shawn Simon
@eagle - I'm sorry, you don't need to use .each for a[href], because everylink's href already placed in "var img_link". Please update
Happy
@eagle - +1 since your solution was already complete. I actually feel a little bad about this one. :(
patrick dw
@patrick: no worries, I like your solution too. We gave OP options, which is always good. Plus, you introduced me to jsfiddle! :D
Senseful
thanks you guys, you are brilliant
Happy
+1  A: 

Here's one way. Use the following regex on the href attribute:

(http://.+/(?=.+"))(.+)

This will match http://site.com/not/similar/links/ and folders/user_picture_name.jpg separately. Then you can combine the two matches with the text appended in the middle.

Shawn Simon
+1  A: 

You can get the attribute by var hrefText = jQuery('a').attr('href'); you can then modify the hrefText as:

hrefText = hrefText.substr(0,hrefText.lastIndexOf('/'))+'user_'+hrefText.substr(hrefText.lastIndexOf('/')),hrefText.length); //do this in each statement and then set the attribute again.

jQuery('a').attr(hrefText);

Having said that if you have number of links you should not go for a javascript approach

sushil bharwani
+2  A: 

Nice and clean. No splits, no arrays, no regular expressions. :o)

Test it here: http://jsfiddle.net/Pjdtm/

    var index = img_link.lastIndexOf('/') + 1;

       // "result" now stores the new href. 
    var result = img_link.substr(0,index) + 'user_' + img_link.substr(index);
patrick dw
+1 for that awesome tool. Plus, I believe that this is the most efficient implementation. You might want to change your jQuery selection to `$('a[href]')`, to get rid of some weird edge cases.
Senseful
@eagle - You mean jsFiddle? Yeah, it is awesome. :o)
patrick dw
@eagle - Didn't see your edit. Yeah, the selector is pretty generic. Not sure if this is more efficient (3 function calls), but is a little prettier than regex. With Happy's last comment under your answer, I'm not sure if we're on the right track at all.
patrick dw
each link's href already stored in var img_link
Happy
please update - it should work with "img_link" variable, not to parse href of the link
Happy
@Happy - They're all in one variable? Are they in an array, or all in one string, or how are they stored?
patrick dw
@Happy - How many are in the variable?
patrick dw
@patrick: yeah I'm confused too, please let me know once you understand what OP wants, and I'll update my answer too.
Senseful
@eagle - I'll do that.
patrick dw
@Happy - Could you please post the content of the variable and whatever other relevant information there is. Your question only made reference to HTML elements. If that is not the case, then you need to post the actual situation.
patrick dw
one img_link = one href
Happy
its used inside other .each, thats why I'm asking to use given variable
Happy
we should parse img_link like it is a href of one link
Happy
@Happy - I'll update my answer in a minute.
patrick dw
@Happy - updated.
patrick dw
@eagle - The *issue* was that Happy was already in an `each()`, and was using a the local variable `img_link` to store the href. Our answers were correct. Mine just needed a minor tweak to make it explicitly accurate. I don't see how you would need to change yours at all.
patrick dw
@patrick: Thanks!
Senseful
@eagle - You're welcome. :o)
patrick dw