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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:16:24+00:00 2026-05-15T09:16:24+00:00

I have an html as follows: <fieldset id=question1> <legend class=’legend’>…</legend> … <input type=text name=label_no1

  • 0

I have an html as follows:

<fieldset id="question1">
<legend class='legend'>...</legend>

...

<input type="text" name="label_no1" id="label_no1" autocomplete="off">

</fieldset>

On the java script, I’m cloning the fieldset, yet I want to access its element to change ids, and some of the text.

I tried this and it did not work:

$('#question1").siblings(".legend").html="any text"; \\ I also tried children

I also want to be able to access all the inputs inside the fieldset, so to change their ids.
Any help?

  • 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-15T09:16:24+00:00Added an answer on May 15, 2026 at 9:16 am

    To clone the fieldset and add it to the same parent:

    var fieldset = $("#question1");    // Get the fieldset
    var clone = fieldset.clone();      // Clone it
    clone.attr("id", "question2");     // Set its ID to "question2"
    clone.appendTo(fieldset.parent()); // Add to the parent
    

    Note we’re changing the ID before adding it to the tree, since you can’t have two elements with the same ID.

    To do things with the elements within it, you can use .children() or .find() on your clone variable with a selector to select the children/descendants you want (once you’ve added the clone to the parent). For instance, to clean up the ids on the inputs:

    clone.find('input').each(function() {
        if (this.id) {
            // It has an ID, which means the original had an ID, which
            // means your DOM tree is temporarily invalid; fix the ID
            this.id = /* ...derive a new, unique ID here... */;
        }
    });
    

    Note that within the each callback, this is not a jQuery instance, it’s the raw element. (Hence my setting this.id directly.) If you wanted to get a jQuery instance for the element, you’d do var $this = $(this); and then use $this.attr("id", ...) instead. But there’s no particular need unless you’re doing something other than changing the ID.


    Answering your question about renumbering IDs, you’ll need to be sure you update whatever’s using those IDs as well as the actual IDs on the input elements.

    But in terms of doing the update on the input elements, you could do it by reading the number and incrementing it until you get one that isn’t used:

    clone.find('input').each(function() {
        var id;
        if (this.id) {
            id = this.id;
            do {
                id = id.replace(/[0-9]+$/g, function(num) {
                    return String(Number(num) + 1);
                });
            }
            while ($("#" + id).length > 0);
            this.id = id;
        }
    });
    

    …which will give you “input_radio2” if the original ID was “input_radio1”, but I think I’d probably use a different naming convention instead. For instance, you could prefix your various input IDs with the ID of the question:

    <fieldset id='question1'>
        ...
        <input id=-'question1:input_radio1' />
    </fieldset>
    

    …and then just replace ‘question1’ with ‘question2’ in the cloned IDs. (Colons [:] are perfectly valid in IDs.)

    If you can avoid having IDs on all of your inputs, though, that would be the way to go. For instance, unless your markup prevents it for a different reason, you can associate an input with its label via containment rather than using for:

    <label>First name: <input type='text' ... /></label>
    

    Lots of options. 🙂

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

Sidebar

Related Questions

I have a HTML structure as follows: <div class=container> <label>Name:</label> <div class=controls> <input type=text
I have an HTML code as follows <body> Hello UserName:<input tyep=text/><br/> Pass:<input type=text/> </body>
I have HTML in the following form: <div id=filters> <ul> <li><label class=checkbox><input type=checkbox value=
I have coded a html page as follows: <html> <head> <meta http-equiv=Content-Type content=text/html; charset=UTF-8
I have html like this <div class=container> <input type=hidden class=myID value=123123123 /> <div class=firstName></div>
I have an editor that built as follows: EditorBuilder builder = context.Html.Telerik().Editor() .Name(ID) .Encode(false)
If I have a drop down list as follows <div class=editor-label> <%= Html.DropDownListFor(model =>
i have the html as follows <div class=element-container id=div_3> <div id=submit_submit class=element> <div class=fleft>
I have the HTML structure as follows: <div class=boxes workshops wrapl> <a href=# id=showw1
I have the HTML structure as follows: <div class=boxes workshops wrapl> <a href=# id=showw1

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.