Skip to content

Instantly share code, notes, and snippets.

@jerrylopez
Created April 25, 2014 15:26
Show Gist options
  • Select an option

  • Save jerrylopez/11293478 to your computer and use it in GitHub Desktop.

Select an option

Save jerrylopez/11293478 to your computer and use it in GitHub Desktop.
This is a simple jQuery plugin that lets you grab the url parameter value and append it to any input you'd like.
// This is a simple jQuery plugin that lets you grab the url parameter value and append it to any input you'd like .
//
// Example:
// Your url is : http://myurl.com/?name=Jerry&last=Lopez&age=21
//
// Now lets say you have a form that you want prefilled when a user visit the form. You can
// can accomplish that with grabbit it will pull the name, last and age parameters then add
// them to the input fields you specify.
//
// Usage:
// $("input.name").grabbit('name');
// $("input.last").grabbit('last');
// $("input.age").grabbit('age');
(function ( $ ) {
$.fn.grabbit = function( urlParam ){
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
return this.val(getParameterByName(urlParam));
};
}( jQuery ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment