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

  • SEARCH
  • Home
  • 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 7163563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:55:25+00:00 2026-05-28T13:55:25+00:00

I have a basic jQuery function going on: $(#selector).click(function(){ $(#target).append(Some text to be added…);

  • 0

I have a basic jQuery function going on:

$("#selector").click(function(){
    $("#target").append("Some text to be added...");
});

That works fine, except I want to append different text on each sequential click. For example:

  1. First click appends text “Message 1”
  2. Second click appends text “Message 2”
  3. Third click appends text “Message 3”

and so on and so forth…

Also, I would like to set a limit, to say, 4 which after 4 clicks, nothing else happens.

Any help would be appreciated.

  • 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-05-28T13:55:26+00:00Added an answer on May 28, 2026 at 1:55 pm
    var messages = [
      'First click appends text "Message 1"',
      'Second click appends text "Message 2"',
      'Third click appends text "Message 3"'
    ];
    
    var i = -1;
    
    var target = $("#target");
    
    $("#selector").click(function(){
        target.append(messages[i = ++i % messages.length]);
    });
    

    This will actually “append” them. If you wanted to replace each message each time, you’d use .text() instead of .append().

    DEMO (using .text()): http://jsfiddle.net/thVK6/

    The i variable will be incremented with each click. When i is equal to messages.length, it will be reset to 0.

    So with each click, i is used to grab a new message from the Array.


    To further explain the increment trick, the % modulo operator returns the remainder when dividing i by messages.length.

    When i is equal to messages.length, the remainder is 0, so we’re back to the start.

    var i = -1;
    

    First click:

    ++i; // 0
    i = i % messages.length; // 0
    
    messages[ i ]; // first message (index 0)
    

    Second click:

    ++i; // 1
    i = i % messages.length; // 1
    
    messages[ i ]; // second message (index 1)
    

    Third click:

    ++i; // 2
    i = i % messages.length; // 2
    
    messages[ i ]; // third message (index 2)
    

    Fourth click:

    ++i; // 3
    i = i % messages.length; // 0, because 3 % 3 === 0
    
    messages[ i ]; // first message (index 0)
    

    …and so i is now 0 again, so it starts over.


    So the same increment trick, but spelled out as above would be…

    $("#selector").click(function(){
        ++i;
        i = i % messages.length;
    
        target.append(messages[ i ]);
    });
    

    http://jsfiddle.net/thVK6/4/

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

Sidebar

Related Questions

I have a fairly basic dialog maker for jquery that works in 2 out
I have some pretty basic jQuery code: ... $(this).find('img').load(function(){ loadedImages++; if(loadedImages == $this.find('img').length){ ...
I currently have a very basic JQuery script accompanied by some CSS that helps
A very basic question... I have a jquery function that fires on page load:
This is a weird one, but... We have this code: jQuery(function($) { $('#basic-modal .basic').click(function(e)
I have the most basic jquery function of them all, but I couldn't find
I have a django project, but for some reason basic jquery isn't working. <html>
I have a basic jQuery selector question. Let's say I'm parsing JSON and generating
Ok so I have a basic function that get's 2 feeds. My issue is
I have written a very basic jquery script that is true theorically but not

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.