Arens Myzyri Arens Myzyri - 9 months ago 49
HTML Question

Select URL parameters with jquery and add them to script

I have a url link:

The parameters are dynamic so it will not be static numbers.
In my example are 4, but maybe less or more.

I have a jquery code that pre selects a dropdown:

jQuery(document).ready(function( $ ) {

Now I want to, depending on the url parameters above to populate jquery code like:

jQuery(document).ready(function( $ ) {

Answer Source

Use regex to form a array of key value pair based on the url and use forEach to loop through the array and add the value to the id using .text()

Hope this helps!

var a = ""

var query = a.split('?')[1] //extract query param
var queries = query.split(/&/g) //extract key value pairs from query param
queries.forEach(function(el){ //loop through the key value pair
  var newElm = '<div id="' + el.split('=')[0] + '">'+el.split('=')[1]+'</div>' //create new elm
  $('body').append(newElm) //append into DOM Body
<script src=""></script>