Click here to Skip to main content
15,888,142 members
Home / Discussions / JavaScript
   

JavaScript

 
AnswerRe: Dynamically add row by DIV Pin
Richard Deeming16-Feb-17 8:40
mveRichard Deeming16-Feb-17 8:40 
GeneralRe: Dynamically add row by DIV Pin
samflex16-Feb-17 9:05
samflex16-Feb-17 9:05 
GeneralRe: Dynamically add row by DIV Pin
Richard Deeming16-Feb-17 9:30
mveRichard Deeming16-Feb-17 9:30 
GeneralRe: Dynamically add row by DIV Pin
samflex16-Feb-17 9:39
samflex16-Feb-17 9:39 
GeneralRe: Dynamically add row by DIV Pin
Richard Deeming16-Feb-17 10:12
mveRichard Deeming16-Feb-17 10:12 
GeneralRe: Dynamically add row by DIV Pin
samflex16-Feb-17 10:27
samflex16-Feb-17 10:27 
GeneralRe: Dynamically add row by DIV Pin
samflex16-Feb-17 10:52
samflex16-Feb-17 10:52 
GeneralRe: Dynamically add row by DIV Pin
Richard Deeming17-Feb-17 2:07
mveRichard Deeming17-Feb-17 2:07 
samflex wrote:
AngularJS

Handlebars. Smile | :)

samflex wrote:
Assume that I have another <div> with ID of addrow1, do just add another handlebars with a separate unique and wrap it around the DIV I need to add the button to?

Effectively, yes.

You'll need some way to know which Handlebars template to use, and which <div> to append it to, based on the button that was clicked.

Something like this should work:
HTML
<script id="row-template" type="text/x-handlebars-template">
<div>
    <input type="hidden" name="rowIDs" value="{{rowNumber}}" />
    ...
    <input id="Button{{rowNumber}}" type="button" rel="remove-row" value="Remove" />
</div>
</script>

<div id="addrow" data-row-count="1" data-template-id="row-template">
    <div>
        <input type="hidden" name="rowIDs" value="1" />
        ...
    </div>
</div>

<input type="button" value="Add More" rel="add-row" data-target="addrow" />

...

<script id="row-template-2" type="text/x-handlebars-template">
<div>
    <input type="hidden" name="row2IDs" value="{{rowNumber}}" />
    ...
    <input id="Button{{rowNumber}}" type="button" rel="remove-row" value="Remove" />
</div>
</script>

<div id="addrow2" data-row-count="1" data-template-id="row-template-2">
    <div>
        <input type="hidden" name="row2IDs" value="1" />
        ...
    </div>
</div>

<input type="button" value="Add More" rel="add-row" data-target="addrow2" />

JavaScript
$(function(){
    var templates = {
        "row-template": Handlebars.compile($("#row-template").html()),
        "row-template-2": Handlebars.compile($("#row-template-2").html())
    };
    
    $("form").on("click", "input[rel='add-row']", function(){
        var containerId = $(this).data("target");
        var container = $("#" + containerId);
        
        var templateId = container.data("templateId");
        var template = templates[templateId];
        
        var rowCount = container.data("rowCount");
        rowCount++;
        
        var context = { rowNumber: rowCount };
        var html = template(context);
        container.append(html);
        
        container.data("rowCount", rowCount);
    });
    
    $("form").on("click", "input[rel='remove-row']", function(){
        var row = this.parentNode;
        var container = row.parentNode;
        container.removeChild(row);
    });
});

Here's a quick demo: Edit fiddle - JSFiddle[^]



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Dynamically add row by DIV Pin
samflex17-Feb-17 2:53
samflex17-Feb-17 2:53 
QuestionInheriting a Javascript Prototype for Web Usercontrol Pin
HemeshSoni13-Feb-17 5:27
HemeshSoni13-Feb-17 5:27 
AnswerRe: Inheriting a Javascript Prototype for Web Usercontrol Pin
Richard Deeming13-Feb-17 8:06
mveRichard Deeming13-Feb-17 8:06 
GeneralRe: Inheriting a Javascript Prototype for Web Usercontrol Pin
HemeshSoni13-Feb-17 17:18
HemeshSoni13-Feb-17 17:18 
AnswerRe: Inheriting a Javascript Prototype for Web Usercontrol Pin
HemeshSoni13-Feb-17 17:25
HemeshSoni13-Feb-17 17:25 
Questionmvc app with angular not working in IIS on 80 port Pin
Member 1103130413-Feb-17 0:17
Member 1103130413-Feb-17 0:17 
AnswerRe: mvc app with angular not working in IIS on 80 port Pin
Nathan Minier13-Feb-17 0:54
professionalNathan Minier13-Feb-17 0:54 
GeneralRe: mvc app with angular not working in IIS on 80 port Pin
Member 1103130413-Feb-17 1:04
Member 1103130413-Feb-17 1:04 
GeneralRe: mvc app with angular not working in IIS on 80 port Pin
Nathan Minier13-Feb-17 1:27
professionalNathan Minier13-Feb-17 1:27 
GeneralRe: mvc app with angular not working in IIS on 80 port Pin
Member 1103130413-Feb-17 2:02
Member 1103130413-Feb-17 2:02 
Questionlooping through an array that has value based on a dice roll. Need to properly calculate the rolls. Pin
Member 1299542011-Feb-17 9:25
Member 1299542011-Feb-17 9:25 
AnswerRe: looping through an array that has value based on a dice roll. Need to properly calculate the rolls. Pin
Richard MacCutchan11-Feb-17 20:45
mveRichard MacCutchan11-Feb-17 20:45 
GeneralRe: looping through an array that has value based on a dice roll. Need to properly calculate the rolls. Pin
Member 1299542012-Feb-17 12:05
Member 1299542012-Feb-17 12:05 
GeneralRe: looping through an array that has value based on a dice roll. Need to properly calculate the rolls. Pin
Richard MacCutchan12-Feb-17 23:05
mveRichard MacCutchan12-Feb-17 23:05 
Questionif (window.location.href.toLowerCase is not working in a Sharepoint site. Pin
RameshLuked9-Feb-17 23:08
RameshLuked9-Feb-17 23:08 
AnswerRe: if (window.location.href.toLowerCase is not working in a Sharepoint site. Pin
Afzaal Ahmad Zeeshan9-Feb-17 23:33
professionalAfzaal Ahmad Zeeshan9-Feb-17 23:33 
AnswerRe: if (window.location.href.toLowerCase is not working in a Sharepoint site. Pin
ZurdoDev10-Feb-17 1:17
professionalZurdoDev10-Feb-17 1:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.