javascript - Change href parameter using jQuery -


how rewrite href parameter, using jquery?

i have links default city

<a href="/search/?what=parks&city=paris">parks</a> <a href="/search/?what=malls&city=paris">malls</a> 

if user enters value #city textbox want replace paris user-entered value.

so far have

var newcity = $("#city").val(); 

given have unique href values (?what=parks, , ?what=malls) suggest not writing path $.attr() method; have have 1 call $.attr() each unique href, , grow redundant, - not mention difficult manage.

below i'm making 1 call $.attr() , using function replace &city= portion new city. thing method these 5 lines of code can update hundreds of links without destroying rest of href values on each link.

$("#city").change(function(o){   $("a.malls").attr('href', function(i,a){     return a.replace( /(city=)[a-z]+/ig, '$1'+o.target.value );   }); }); 

one thing may want watch out spaces, , casing. convert lower case using .tolowercase() javascript method, , can replace spaces call .replace() i've down below:

'$1'+o.target.value.replace(/\s+/, ''); 

online demo: http://jsbin.com/ohejez/


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -