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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:52:35+00:00 2026-05-12T00:52:35+00:00

I am using ASP.Net MVC along with Jquery to create a page which contains

  • 0

I am using ASP.Net MVC along with Jquery to create a page which contains a contact details section which will allow the user to enter different contact details:

<div id='ContactDetails'>
    <div class='ContactDetailsEntry'>
        <select id="venue_ContactLink_ContactDatas[0]_Type" name="venue.ContactLink.ContactDatas[0].Type">
            <option>Email</option>
            <option>Phone</option>
            <option>Fax</option>
        </select>
        <input id="venue_ContactLink_ContactDatas[0]_Data" name="venue.ContactLink.ContactDatas[0].Data" type="text" value="" />
    </div>
</div>
<p>
    <input type="submit" name="SubmitButton" value="AddContact" id='addContact' />
</p>

Pressing the button is supposed to add a templated version of the ContactDetailsEntry classed div to the page. However I also need to ensure that the index of each id is incremented.

I have managed to do this with the following function which is triggered on the click of the button:

function addContactDetails() {
    var len = $('#ContactDetails').length;
    var content = "<div class='ContactDetailsEntry'>";
    content += "<select id='venue_ContactLink_ContactDatas[" + len + "]_Type' name='venue.ContactLink.ContactDatas[" + len + "].Type'><option>Email</option>";
    content += "<option>Phone</option>";
    content += "<option>Fax</option>";
    content += "</select>";
    content += "<input id='venue_ContactLink_ContactDatas[" + len + "]_Data' name='venue.ContactLink.ContactDatas[" + len + "].Data' type='text' value='' />";
    content += "</div>";
    $('#ContactDetails').append(content);
}

This works fine, however if I change the html, I need to change it in two places.

I have considered using clone() to do this but have three problems:

EDIT: I have found answers to questions as shown below:

  1. (is a general problem which I cannot find an answer to) how do I create a selector for the ids which include angled brackets, since jquery uses these for a attribute selector.
    EDIT: Answer use \ to escape the brackets i.e. $('#id\\[0\\]')
  2. how do I change the ids within the tree.

EDIT: I have created a function as follows:

function updateAttributes(clone, count) {
    var f = clone.find('*').andSelf();
    f.each(function (i) {
        var s = $(this).attr("id");
        if (s != null && s != "") {
            s = s.replace(/([^\[]+)\[0\]/, "$1[" + count + "]");
            $(this).attr("id", s);
        }
    });

This appears to work when called with the cloned set and the count of existing versions of that set. It is not ideal as I need to perform the same for name and for attributes. I shall continue to work on this and add an answer when I have one. I’d appreciate any further comments on how I might improve this to be generic for all tags and attributes which asp.net MVC might create.

  1. how do I clone from a template i.e. not from an active fieldset which has data already entered, or return fields to their default values on the cloned set.
  • 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-12T00:52:36+00:00Added an answer on May 12, 2026 at 12:52 am

    So, I have a solution which works in my case, but would need some adjustment if other element types are included, or if other attributes are set by with an index included.

    I’ll answer my questions in turn:

    1. To select an element which includes square brackets in it’s attributes escape the square brackets using double back slashes as follows: var clone = $(“#contactFields\[0\]”).clone();

    2. & 3. Changing the ids in the tree I have implemented with the following function, where clone is the variable clone (in 1) and count is the count of cloned statements.

      function updateAttributes(clone, count) {
      var attribute = [‘id’, ‘for’, ‘name’];
      var f = clone.find(‘*’).andSelf();
      f.each(function(i){

                      var tag = $(this);
      
              $.each(attribute, function(i, val){
      
                      var s = tag.attr(val);
                      if (s!=null&& s!="")
                          {
                          s = s.replace(/([^\[]+)\[0\]/, "$1["+count+"]");
                          tag.attr(val, s);
                          }
              });
       if ($(this)[0].nodeName == 'SELECT')
       { $(this).val(0);}
       else
       {
           $(this).val("");
       }
      });
      

      }

    This may not be the most efficient way or the best, but it does work in my cases I have used it in. The attributes array could be extended if required, and further elements would need to be included in the defaulting action at the end, e.g. for checkboxes.

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

Sidebar

Related Questions

I'm using the DataAnnotations attributes along with ASP.Net MVC 2 to provide model validation
I'm using the ImageResizer module in an ASP.NET MVC 4 project, along with the
Using ASP.NET MVC + jQuery : I need to use some values owned by
i am using asp.net mvc 3. in one my of page. i need to
I am using jquery 1.3.2 and asp.net mvc 1.0. Originally I was using some
using ASP.NET MVC 2 I have a navigation menu inside my Master Page. In
I am using AutoMapper in my project along with, NHibernate and ASP.NET MVC 2.
I have an ASP.NET MVC 3 app and I'm using jQuery DataTables as grid.
I'm using ASP.NET MVC 1.0 along with the Oracle ASP.NET Membership Providers. I'm running
I am using ASP.NET MVC along with JQueryMobile in a web app. I want

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.