1

I'm using this snippet of code to grab a url from a clicked link:

var url = $(this).find('a').attr('href');

I want to add a string to that url.

For example, my current url is:

http://mydomain.com/myarticle

I want to make it:

http://mydomain.com/myarticle-chinese

What should I add to my initial line of code to make that happen?

I'd be grateful for your advice!

ADDENDUM: THANK YOU VERY MUCH! FOUR PEOPLE ANSWERED ALMOST SIMULTANEOUSLY AND ALL FOUR GAVE ME A HELPFUL ANSWER. I WISH I COULD ACCEPT ALL FOUR!

Dimitri Vorontzov
  • 7,834
  • 12
  • 48
  • 76

4 Answers4

3
var $a = $(this).find('a');
var url = $a.attr('href');
$a.attr('href', url + '-chinese');
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
2
var url = $(this).find('a').attr('href') + "-chinese";
Saad Imran.
  • 4,480
  • 2
  • 23
  • 33
2

This will store the new value in url:

var url = $(this).find('a').attr('href') + '-chinese';

and this will redirect the user's browser:

window.location.href = $(this).find('a').attr('href') + '-chinese'
George Cummins
  • 28,485
  • 8
  • 71
  • 90
1

You want to append the string "-chinese" to the retrieved URL? Try this:

var url = $(this).find('a').attr('href') + '-chinese';
Jon Gauthier
  • 25,202
  • 6
  • 63
  • 69