31. Sending url via javascript

We will utilize javascript to fill in the '#' from the href section with the 'name' parameter. In order to do this, we will edit the main.js file

main.js
/*
the url is a instance of the URL class
document.URL gets the current URL
*/
var url = new URL(document.URL);
var items = document.getElementsByClassName("item-order"); //returns a list of the three href elements

for (i = 0; i < items.length; i++)
{
    url.searchParams.set("order", items[i].name); //(name, value)
    items[i].href = url.href;
};

In the JavaScript code above, we first define a url variable to hold the URL class instance of the current URL. We then create an items list to store the href attributes of elements with the class name item-order. To update the URL by appending a parameter based on the user's selected order type, we iterate over each URL in the items list. For each URL, we set a new parameter using its name and update the href attributes of the corresponding HTML elements with the newly constructed URL.

If your url is finishing with a #, just manually delete it directly from the url and refresh the page

Last updated