-4

I have a select option like this:

<select class="sortBybUTT">
  <option value="0" disabled selected>Sort by</option>
  <option value="1">Highest price</option>
  <option value="2">Lowest Price</option>
  <option value="3">Most Recent</option>
  <option value="4">Most popular</option>
  <option value="5">Discount</option>
</select>

What I'm trying to do is to send data to JavaScript every time I change it without a submit button.

After I take the data to create a URL and redirect with JavaScript.

Example: data = Lowest Price = value 2: my_shop.html?sort_by=2

Aalexander
  • 4,987
  • 3
  • 11
  • 34
proggg
  • 3
  • 2

2 Answers2

0

You could use an event listener then get the data.

let select = document.getElementsByClassName("sortBybUTT")[0];

select.addEventListener("change", function () {
    let data = select.value;
});

These might help you out:

  • The above code doesn't work, because the variable "select" is undefined due to the fact that the select tag is using the **class** "sortBybUTT" and not the id. – Kai Jan 02 '21 at 14:36
0

Use the onchange event.

let element = document.getElementsByClassName("sortBybUTT")[0];
element.addEventListener("change", () => {
    console.log(element.value);
});
Kai
  • 323
  • 2
  • 10