Note that there are some explanatory texts on larger screens.

plurals
  1. POadd templated div to page multiple times with different ids
    primarykey
    data
    text
    <p>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: </p> <pre><code> &lt;div id='ContactDetails'&gt; &lt;div class='ContactDetailsEntry'&gt; &lt;select id="venue_ContactLink_ContactDatas[0]_Type" name="venue.ContactLink.ContactDatas[0].Type"&gt;&lt;option&gt;Email&lt;/option&gt; &lt;option&gt;Phone&lt;/option&gt; &lt;option&gt;Fax&lt;/option&gt; &lt;/select&gt; &lt;input id="venue_ContactLink_ContactDatas[0]_Data" name="venue.ContactLink.ContactDatas[0].Data" type="text" value="" /&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt; &lt;input type="submit" name="SubmitButton" value="AddContact" id='addContact'/&gt; &lt;/p&gt; </code></pre> <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. </p> <p>I have managed to do this with the following function which is triggered on the click of the button:</p> <pre><code>function addContactDetails() { var len = $('#ContactDetails').length; var content = "&lt;div class='ContactDetailsEntry'&gt;"; content += "&lt;select id='venue_ContactLink_ContactDatas["+len+"]_Type' name='venue.ContactLink.ContactDatas["+len+"].Type'&gt;&lt;option&gt;Email&lt;/option&gt;"; content += "&lt;option&gt;Phone&lt;/option&gt;"; content += "&lt;option&gt;Fax&lt;/option&gt;"; content += "&lt;/select&gt;"; content += "&lt;input id='venue_ContactLink_ContactDatas["+len+"]_Data' name='venue.ContactLink.ContactDatas["+len+"].Data' type='text' value='' /&gt;"; content += "&lt;/div&gt;"; $('#ContactDetails').append(content); } </code></pre> <p>This works fine, however if I change the html, I need to change it in two places. </p> <p>I have considered using clone() to do this but have three problems: </p> <p><strong>EDIT: I have found answers to questions as shown below:</strong> </p> <ol> <li>(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. <strong>EDIT:</strong> Answer use \ to escape the brackets i.e. <code>$('#id\\[0\\]')</code></li> <li>how do I change the ids within the tree.</li> </ol> <p><strong>EDIT:</strong> I have created a function as follows: </p> <pre><code>function updateAttributes(clone, count) { var f = clone.find('*').andSelf(); f.each(function(i){ var s = $(this).attr("id"); if (s!=null&amp;&amp; s!="") { s = s.replace(/([^\[]+)\[0\]/, "$1["+count+"]"); $(this).attr("id", s); }}); </code></pre> <p>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. </p> <ol start="3"> <li>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. </li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload