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 6558215
In Process

The Archive Base Latest Questions

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

var x = 0; var counter = 0 ; $(function () { $(‘#addBtn’).click(function ()

  • 0
 var x = 0;
    var counter = 0 ;

    $(function () {
        $('#addBtn').click(function () {

                        x++;

                       if (counter < 5) {
                counter++;
                $('#content').append('<input type="text" id="mytxt' + x + '">');
                $('#content').append('<input type="button" id="removeBtn' + x + '" value="Remove" onclick="removeRow(' + x + ')" />');
                $('#content').append('<div id="br' + x + '"/></div>');



            } else {
                alert("you cannot added more than 5 element");
            }


        }
        );



    });




    function removeRow(index) {
        $('#mytxt' + index).remove();
        $('#removeBtn' + index).remove();
        $('#br' + index).remove();
        counter--;
           alert(counter);

    }

this is my function to create dynamic button, when i clicked “addBtn”, new element will be created and id start with 1,eg: mytxt1, and when i clicked “removeBtn” and “addBtn” again,the id will become mytxt2, the result is out of my expected,
what i want is when i clicked “removeBtn” and “addBtn” ,the id will start from 1 again,but not 2

new updated

that is one of my question also, if i added more element let said 4 element,the id i get will be mytxt1,mytxt2,mytxt3 and mytxt4, and if i remove mytxt2, the next element i added will become mytxt1,mytxt3,mytxt4,and mytxt5, and this is not what i want,what i want is mytxt1,mytxt2,mytxt3 and mytxt4

  • 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-25T13:10:20+00:00Added an answer on May 25, 2026 at 1:10 pm

    The following (untested) code keeps track of which indexes have been used, and allows reuse of indexes formerly associated with removed items. Note that the user could remove items in any order; this code reuses lower indexes first.

    var maxElements = 5,
        indexes = [];
    
    $(function () {
       $('#addBtn').click(function () {
          // look for a false or undefined index in the array
          // note: index numbers will start at 1 as in the question
          for (var x=1; x <= maxElements; x++) {
             if (!indexes[x]) {
                // found a currently unused index, so use it, and mark as used
                indexes[x] = true;
    
                $('#content').append('<input type="text" id="mytxt' + x + '">');
                $('#content').append('<input type="button" id="removeBtn' + x 
                            + '" value="Remove" onclick="removeRow(' + x + ')" />');
                $('#content').append('<div id="br' + x + '"/></div>');
    
                return;
             }
          }
    
          // no unused index was found
          alert("You cannot added more than " + maxElements + " elements.");
       });
    });
    
    function removeRow(index) {
       $('#removeBtn' + index).remove();
       $('#mytxt' + index).remove();
       $('#br' + index).remove();
    
       // mark index as not used
       indexes[index] = false;
    }
    

    EDIT: Note, I kept this as similar to your starting code as I could, but I removed the counter variable entirely because my code doesn’t use it – obviously you can put it back (with corresponding counter++ and counter-- in the appropriate places) if you want instant access to the current count of elements.

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

Sidebar

Related Questions

i have script that append sections(div) to my page $(document).ready(function () { var counter
Here is the code: //Mouseover start countdown $(#icon_no_1).mouseover(function() { $(this).fadeTo(slow, 0.23); //Countdown var counter
TheObject = { getArray: function(){ var groups = new Array; $.ajax({ type: "POST", url:
I have the following code: $(input[id$=UserField_hiddenSpanData],input[title=Title]).each(function(){ var rb = $('#ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_SelectResult option').length; var val =
Consider the following HTML table: <table id=myTable1> <tr id=TR1> <td><input type=text id=quantity1 name=quantity1 /></td>
var y = new w(); var x = y.z; y.z= function() { doOneThing(); x();
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
So, I am seeing a curious problem. If I have a function // counter
function drawinventoryList() { inventoryArray.sort(); var inventoryString = ; for (x in inventoryArray) { arrayValue
I have a set of global counter variables in Javascript: var counter_0 = 0;

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.