Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
I need to get this fixed Yesterday?? – Dhaval Marthak Apr 24 at 9:20
possible duplicate of How to get the value from URL Parameter? – Quentin Apr 24 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 – R4N_S_S Apr 24 at 10:08

3 Answers

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, " "));
}
share|improve this answer
I tested it and it works on all browsers. Also your input element must end with /> – Elio Apr 24 at 11:30

Try this:

// Get the url parameter from location href
$.getCurrency= function(name){
    var results = new RegExp('[\\?&amp;]' + name + '=([^&amp;#]*)').exec(window.location.href);
    return results[1] || 0;
}

// Set the input text box value
$("#currencyrequired").val(decodeURIComponent($.getCurrency('currency')));
share|improve this answer
Thanks Palash but this didnt seem to work :( – R4N_S_S Apr 24 at 10:09

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/

share|improve this answer
1  
That will only work if currency=Denmark is the last information in the URL. – TryingToImprove Apr 24 at 9:30
This works in the jsfiddle but when i aste the code into my page it doesnt do anything. – R4N_S_S Apr 24 at 10:13
@R4N_S_S - do you have jquery defined on your page? Try adding an alert. alert(country); - does the alert work? – Darren Davies Apr 24 at 10:16
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/… – R4N_S_S Apr 24 at 10:25
@Darren - yes jquery is present on the page. the alert works - thanks in advance – R4N_S_S Apr 24 at 10:30
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.