I have a URL with a parameter, as shown below.
http://example.html?name=Sport
To retrieve the parameter I use the following lines of code:
let url_string = window.location.href;
let url2 = new URL(url_string);
let name_url = url2.searchParams.get("name");
And so far there are no problems.
I want this parameter present in the url to be used as an option for the select, therefore already pre-set to the value that corresponds to the parameter we pass in the URL (in our case on Sport).
I tried to search on the internet but the examples I found are shown in jQuery but I only need it in Javascript.
Can anyone help me to do this?
Assign name_url to the .value property of the select.
let url_string = window.location.href;
let url2 = new URL(url_string);
let name_url = url2.searchParams.get("name");
document.getElementById("your-select").value = name_url;
Replace your-select with the ID of the <select> that you want to preset.