-2

Is there a clear cut method to changing the contents of a url in href based on user submitted information?

For EXAMPLE: A user submits the word "basketball" into a search box and the result is the following link.

<a href="http://google.com/basketball>More information</a>

If the user submitted lets say "football" the link would then change to:

<a href="http://google.com/football>More information</a>
JVulture
  • 11
  • 1
  • 2
  • because its a duplicate [How to change the href for a hyperlink using jQuery](http://stackoverflow.com/questions/179713/how-to-change-the-href-for-a-hyperlink-using-jquery) – arun Nov 16 '12 at 05:34
  • You can see the answer here... It's a duplicate question.... [http://stackoverflow.com/questions/179713](http://stackoverflow.com/questions/179713/how-to-change-the-href-for-a-hyperlink-using-jquery) – Ofir Hadad Nov 16 '12 at 05:29

2 Answers2

2

You have missing close quote of href, You can do it this way.

Live Demo

var url = $('#aId').attr('href');
firstPart = url.substring(0, url.lastIndexOf('\/')+1);
$('#aId').attr('href', firstPart + "Football");
Adil
  • 146,340
  • 25
  • 209
  • 204
1

Given: <a id="url" href="http://google.com/basketball">More information</a> <input id="sport" type="text" val="soccer" />

var $val = $('#sport').val()
$("#url").attr("href", "http://www.google.com/" + $val);
The Internet
  • 7,959
  • 10
  • 54
  • 89