0

I have a Javascript event that is fired when a location is detected. Let's say that the detected location is "Melbourne Australia".

I want to change the following:

<a href="/search?location=&input=Suits">Suits</a> 

To be:

<a href="/search?location=Melbourne Australia&input=Suits">Suits</a> 

All I can think of us changing the entire href value. Is there a 'sane' way to change only the value of location= when the event is fired.

1 Answer 1

2

give a id to your link

<a href="/search?location=&input=Suits" id="my_link">Suits</a> 

and use js to modify it

var your_location = "Melbourne Australia"; // can be dynamic

var obj = document.getElementById('my_link');

var h = obj.href;

var string_to_find = "location=";
var string_to_replace = "location="+your_location;
var new_h = h.replace(string_to_find, string_to_replace);

obj.href=new_h;
Sign up to request clarification or add additional context in comments.

Comments

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.