Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8325349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:24:17+00:00 2026-06-09T00:24:17+00:00

When button is clicked in my script it brings back an image file (#mypic),

  • 0

When “button” is clicked in my script it brings back an image file (“#mypic”), a sound file (“#mysoundclip”) and a class that styles a word to spell (“#picknext”).

The thing is, I want the button to repeat its action 3 times before moving on to the next image, sound and style. How would I do this?

$('#pickNext').click(function() {
// remove the class from all td's
$('td').removeClass('spellword');
// pick a random word
var r = rndWord;
while (r == rndWord) {
    rndWord = Math.floor(Math.random() * (listOfWords.length));
}
// apply class to all cells containing a letter from that word
$('td[data-word="' + listOfWords[rndWord].name + '"]').addClass('spellword');

});


var audio = $("#mysoundclip")[0];
var i  = 0;
$(".minibutton").click(function() {
var noExist = $('td[data-word=' + listOfWords[rndWord].name + ']').hasClass('wordglow2');
if (noExist && i >= 3) {
    $('#pickNext').click();
    i = 0;
} else {

    $("#mysoundclip").attr('src', listOfWords[rndWord].audio);
    audio.play();
    pic = $("#mypic").attr('src', listOfWords[rndWord].pic);
    pic.show();
    $i++;
}
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:24:19+00:00Added an answer on June 9, 2026 at 12:24 am

    Keep track of how many times the button has been pressed using jQuery’s .data() function. You’d set a counter and then increment, doing things when it hits 3. A basic outline:

    timesClicked = $(this).data('timesClicked');
    if(!timesClicked) { timesClicked = 0 } //Allow for the first time
    if(timesClicked < 3) {
      //Increment the counter
      $(this).data('timesClicked',timesClicked+1);
    } else {
      $(this).data('timesClicked',0);
      //Do your action
    }
    

    If I understand your code, it would look something like this:

    if (noExist) {
        $(this).data('timesClicked', 0);
        $(this).click();
    } else {
        timesClicked = $(this).data('timesClicked');
        if(timesClicked < 3) {
            //Increment the counter
            $(this).data('timesClicked',timesClicked+1);
        } else {
            //Reset the counter and pick new things
            $(this).data('timesClicked',0);
            $("#mysoundclip").attr('src', listOfWords[rndWord].audio);
            pic = $("#mypic").attr('src', listOfWords[rndWord].pic);
        }
        //Either way, play the sounds       
        audio.play();
        pic.show();
    }
    

    This will allow you to add more buttons if you want to, and in any event is good coding practice. It may be wise to also add a ‘maxClicks’ data attribute that is set to ‘3’ that gets checked, so that you can vary how many clicks it needs.

    Looking at the code you put up your jsFiddle, there are several things that need to be fixed. Notably, you seem to be confusing the “type” attribute of your inputs with the id. Each id should only be used once, you’re using pickNext twice. That will break all kinds of things. You also are using a type of “button2”, which isn’t valid. Your $(‘button’) and $(‘button2’) click events won’t work as you anticipated, because those are looking for elements with the tag of and . So the first event will be fired any time any button is pressed, and the second never will. The $(‘#pickNext’) click event, since you’re using pickNext twice, will probably only fire for the second button. You should probably give them id’s of “pickNext” and “reset”, and then refer to those selectors for your click events.

    Long story short: get the rest of your code working, and then worry about getting things to happen three times.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on classic asp script, which when create file button is clicked
I would like to handle a button clicked event in a native c++ class.
I have this label that i want to fadeOut after button event clicked. I
Right now I have a script that will update once the submit button is
I have a PHP script that produces a .CSV export file from a MySQL
With the script below I append 3 elements when the append button is clicked.
I'm trying to add an image after a button is clicked. However, I can't
I have a JQuery Dialog that is opened when a button is clicked. The
I have a javascript function which runs when a script button is clicked, //script
I hava set up AJAX script that when you click the button it calls

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.