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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:51:03+00:00 2026-06-14T07:51:03+00:00

I have 2 main issues with my clone() script. Whenever I clone a div,

  • 0

I have 2 main issues with my clone() script.

  1. Whenever I clone a div, I’d like to change the text with the increment value. Eg, the new block should have the title “Block 2″… and so on.
  2. When I click on the radio button and clone it, the cloned block still has the radio selected. I already have the value set to empty value in the script but it does not seem to be working on the first clone.

HTML:

<div>
    <div id="mydiv">
        <div class="title">Block 1</div>
        <div>
            <input type="radio" name="blockoption" value="left"> Radio 1<br />
            <input type="radio" name="blockoption" value="right"> Radio 2<br />
            <select>
                <option selected="selected">Select</option>
                <option>Option 1</option>
                <option>Option 2</option>
                <option>Option 3</option>
            </select><br />
            <input type="text" name="blocktext" />
        </div>
    </div>
</div>
<input id="addbutton" type="button" value="Add Block" />&nbsp;<input id="removebutton" type="button" value="Remove Block" />

jQuery:

$("#addbutton").click(function () {
var inc = $('#mydiv').siblings().length;
$('#mydiv:first-child').clone().insertAfter('#mydiv:last-child').find('input').val('');
$('#mydiv').find('.title').each(function() {
    $(this).text().replace(/\d+/, inc+2);
});
});

$("#removebutton").click(function () {
var inc = $('#mydiv').siblings().length;
if (inc > 0) {
    $('#mydiv:last-child').remove('#mydiv');
    $('#mydiv').find('.title').each(function() {
        $(this).text().replace(/\d+/, inc-2);
    });
}
});

See the code in action here.

  • 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-14T07:51:03+00:00Added an answer on June 14, 2026 at 7:51 am

    Cloning an element that has ID attribute makes your document invalid you can use classes instead. For removing checked and selected properties you can use prop method.

    $("#addbutton").click(function() {
        var inc = $('.mydiv').length;
        var cloned = $('.mydiv:first').clone()
            .find('.title').text('Block' + ++inc).end()
            .find('input').val('').prop('checked', false).end()
            .find('option').prop('selected', false).end()
            .appendTo('.wrapper')
    });
    
    $("#removebutton").click(function() {
        $('.mydiv:last').remove()
    });
    

    http://jsfiddle.net/b7knT/

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

Sidebar

Related Questions

I have a main-section div that is set to inherit it's height from its'
I am new to android development and I have been having issues with Force
When I execute another script from my main script like so: #!/bin/bash if cond1;
I have issues when trying to place an event to toggle a div using
I have one main outstanding issue, I know now how to databind to lists
I'm trying to solve an issue with makefile. I have a main.cpp and then
I have main window which has inner grid components. When I press a button
I have main categories and sub categories. I want to be able to sort
I have main table called 'Employee' and another slave table called 'EmployeeTypes' that has
i have main activity in which i have Four menus. and i have one

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.