0

On WordPress I'm switching between categories of posts with a dropdown, and have a "load more" button that loads more posts into the selected category. Each time the button is pressed, a variable is updated, so WordPress knows whether to load the 2nd, 3rd, 4th tranche of posts in that category.

The counter ought to reset when the dropdown is changed though - and I can't figure out how to do it.

To simplify, if you have:

<select id="selection">
  <option value="foo"></option>
  <option value="bar"></option>
</select>

<button id="button"></button> 

I want to to get a string of alerts - one per button click - saying "foo1", "foo2" // "bar1", "bar2" // "foo1", "foo2", "foo3".

My code doesn't quite work though - after a while changing the dropdown I get multiple alerts for a single button click.

$(document).ready(function(){
    $("#selection").on('change', function(){
         myval = $("#selection").val();
         count = 0; 
         $("button").on('click', function(){
             count++;
             alert(myval + count);
        })
     })
 })

How can I completely wipeout my count value when the selector changes?

closed as off-topic by Max Yudin, Jacob Peattie, rudtek, fuxia Jan 4 at 22:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

1

You can do this with each function by creating the array and push the selected value into it.

Html

<select id="selection">
  <option value="foo"></option>
  <option value="bar"></option>
</select>

Jquery

 var arrayOfValues = new Array();
    $("select").each(function(i,obj){
     count = 0; 
        $(obj).change(function() {
             $("select").each(function(i,obj){
             count++;
                arrayOfValues.push($(obj).find(":selected").val()+count);         
             });
            alert(arrayOfValues);
        });
    });

Hope this will help you and also let me know the result.

Not the answer you're looking for? Browse other questions tagged or ask your own question.