Reset counter with jQuery

admin2025-06-05  3

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

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. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

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?

Share Improve this question asked Dec 21, 2018 at 11:21 JohnGJohnG 3443 silver badges17 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749077969a316171.html

最新回复(0)