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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:59:17+00:00 2026-05-15T16:59:17+00:00

I’m trying to change the id of my elements when I clone an element,

  • 0

I’m trying to change the id of my elements when I clone an element, basically I keep a counter that I increment and add it at the end of the id each time it’s cloned. Seems simple enough.

var addAnswerFlag = true;
function addAnswer(button)
{
    //flag to keep clicks from chaining
    if(addAnswerFlag)
    {
        addAnswerFlag = false;
        $('#answer_warning').fadeOut(600);
        $('.template').clone().attr('id','').insertAfter($('.template')).fadeIn(300, function() {

            $(this).removeClass('template'); 
            addAnswerFlag = true;

            $('.answer_li:not(.template)').each(function(index){
                ++index;

                $(this).children('.answer').each(function(){
                    $(this).attr('id', $(this).attr('id').replace("__", "_"+index+"_")).attr('name',$(this).attr('name').replace("__", "_"+index+"_"));         
                });

            });

        });

    }
}

Sorry if my code is a little unclear.

Basically my first loop goes through all my list elements (except for the template one with dummy data) and takes each ‘.answer’ and replaces the double underscore “__” for “i” where i is a counter.

For some dang reason i is always 1. I know the value increments for each list item because if I alert its value before and after I attempt the reassigning of the id the value goes up but in the actual assignment it is always 1.

This is where I get extremely confused. Any help would be appreciated 🙂

Edit:

Here’s some HTML. Basically I’m looping through my list items here and changing the IDs and names of my inputs so I can handle them once they are submitted:

<ul id="answers">
    <li style="" class="answer_li template">
        <input id="question__en" name="question_[en]" class="answer" value="" type="text">
        <input id="question__fr" name="question_[fr]" class="answer" value="" type="text">              
    </li>
    <li id="" style="display: list-item;" class="answer_li">
        <input id="question_1_en" name="question_[en]" class="answer" value="" type="text">
        <input id="question_1_fr" name="question_[fr]" class="answer" value="" type="text">             
    </li>
</ul>

As you can see the first one is my template and the second has the index added between the underscores. However as I clone the template they all get the same index. I really don’t understand because if I alert the value it’s incremented correctly, I was so confused that I went back into basic javascript tutorial to see if I was missing some sort of operator that I was using by accident 🙂

I also posted the whole javascript function that gets called when I click on a button (which I pass as a parameter).

  • 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-15T16:59:17+00:00Added an answer on May 15, 2026 at 4:59 pm

    The each function will supply the index (zero-based) so there’s no need to keep a separate variable. Note that index will be a fresh value for each iteration, so you don’t need to reason about whether the closure is correct or not.

    $('.answer_li:not(.template)').each( function(index){
        ++index; // to get one-based values
        $(this).children('.answer').each( function(){
            $(this).attr('id', $(this).attr('id').replace("__", "_"+index+"_")).attr('name',$(this).attr('name').replace("__", "_"+index+"_"));         
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

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.