-1

so with jQuery I'd like to take my url value example:

/finance-proposal?value=BD07UWT

And add it to this input element on my page:

<input name="field[21]" id="field21" class="input-text required-entry " style="" value="" type="text"></input>

I have tried to parse the url value however I have had no success.

Any idea what would be the most simple method of doing this with jQuery when the page loads?

Thanks, Nick

isherwood
  • 58,414
  • 16
  • 114
  • 157
Nick M
  • 187
  • 2
  • 4
  • 8
  • There are plugins available for capturing query parameters. Then you'd use `val()` to set the input. – isherwood Nov 06 '15 at 13:11
  • 1
    Have a look here http://stackoverflow.com/questions/5448545/how-to-retrieve-get-parameters-from-javascript – Tree Nguyen Nov 06 '15 at 13:13
  • Use a good URL parsing library. For example, this one: https://github.com/allmarkedup/purl Your current location is stored in window.location. – nagylzs Nov 06 '15 at 13:15

1 Answers1

1

Use getParameterByName(How can I get query string values in JavaScript?) to get value from querystring provided below. You can assign value using .val()

var getParameterByName = function(name) {
  name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
  return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
};
var value = getParameterByName('finance-proposal');
$('.input-text required-entry').val(value);
Community
  • 1
  • 1
Rayon
  • 36,219
  • 4
  • 49
  • 76