Regex - Show selected option by URL parameter

0 votes
276 views
added Jul 3, 2018 in Regex by LC Marshal Captain (25,790 points)
edited Jul 13, 2018 by LC Marshal
//example of URL parameter property.php?action=buy

//use this function to get the parameter value:
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
}

//Then use the following method to set the value:
var val = getURLParameter('buy');
$('input#buy').val(val);   //  assign URL param to select field
lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...