I have a submit button which calls a specific method based on the selected <option> by the user.
The problem is both the previous and current addEventListener with their corresponding method gets called when I press the submit button. Both function one and two gets executed. I only want to execute functiontwo if the dropdown value is 2 and the user presses the submit button. What am I doing wrong?
HTML:
<select class="div-toggle" id="filter-list" data-target=".my-info-1">
<option value="1" data-show=".1">Option 1</option>
<option value="2" data-show=".2">Option 2</option>
</select>
<button id="submitbutton">Submit</button>
JQuery/JavaScript:
I use the if pathname here because I use one general JavaScript file. If I declare the eventlistener outside functions I get undefined error on other pages because it can't find the element.
var btnsubmit = document.getElementById("submitbutton");
if (window.location.pathname=='/test/testpage.html') {
$('#filter-list').on('change', function() {
if(this.value == '1'){
btnsubmit.addEventListener('click', functionOne);
} else if(this.value == '2'){
btnsubmit.addEventListener('click', functionTwo);
}
});