2

I have a url

http://www.example.com/state=survey&action=display&survey=39&zone=surveys&currency=Denmark

A user will land on a new page with the above url. This page will include a form. There is an input with an id 'currencyrequired' - i want this input to automatically pull in the currency variable from the url (in this example Denmark).

<input type="text" id="currencyrequired" name="currencyrequired" size="40" maxlength="20" value="" class="input-pos-int">

The currency part of the url is likely to change as it depends upon which country they select so you could end up with - http://www.example.com/state=survey&action=display&survey=39&zone=surveys&currency=Norway

Ideally what i would like is to list all the currencies (8 in total) and the script would check what country is listed in the url and populate the currencyrequired input field with the output

3
  • I need to get this fixed Yesterday?? Commented Apr 24, 2013 at 9:20
  • possible duplicate of How to get the value from URL Parameter? Commented Apr 24, 2013 at 9:26
  • @TheVal - this project is due this week and i need to get this functionality rolled out asap hence the urgency. Im a relative novice with javascript/jquery so any help is appreciated Commented Apr 24, 2013 at 10:08

2 Answers 2

0

You could use location.search to return the query string and do:

var url = location.search;
var country = url.substring(url.indexOf('currency=')+9, url.length);

$("#input").val(country);

http://jsfiddle.net/ckJ5S/

Sign up to request clarification or add additional context in comments.

6 Comments

That will only work if currency=Denmark is the last information in the URL.
This works in the jsfiddle but when i aste the code into my page it doesnt do anything.
@R4N_S_S - do you have jquery defined on your page? Try adding an alert. alert(country); - does the alert work?
The currency part of the url is likely to change as it depends upon which country they select so you could end up with - example.com/…
@Darren - yes jquery is present on the page. the alert works - thanks in advance
|
0

Try this:

$("#currencyrequired").val(getParameterByName('currency'));

function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

1 Comment

I tested it and it works on all browsers. Also your input element must end with />

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.