Pregunta

I have a div that has multiple child divs, or panels, that on page load, have styles appended to them placing them in the correct way on the page. I have a button that adds a new panel, but that panel doesn't get the necessary styles added to it. (The method is the jQuery insertAfter function). I am using the Syrena Admin template, so I do not know the function to call to re-parse the elements. Is there a way to reload the div as if the page was reloading? In other words is there a way to reload only part of the page?

HTML

<div class="magic-layout isotope">
    <div id="panel2" class="panel panel-default magic-element isotope-item">
        <div class="panel-heading">
            <span class="panel-title">Additional</span>
        </div>
        <div class="panel-body">
            <div class="form-group">
                <input type="text" name="role[]" placeholder="Role" class="form-control" display="text" /> 
                <input type="text" name="first_name[]" placeholder="First Name" class="form-control"  display="text"/>

                <input type="text" name="last_name[]" placeholder="Last Name" class="form-control"  display="text"/>
                <input type="text" name="phone[]" placeholder="Phone Number" class="form-control" display="text" />
            </div>
            <div class="form-group">
                <input type="text" name="address_line_1[]" placeholder="Address Line 1" class="form-control"  display="text"/>
                <input type="text" name="address_line_2[]" placeholder="Address Line 2" class="form-control"  display="text" value=""/>
                <input type="text" name="line_3[]" placeholder="City, State Zip" class="form-control"  display="text"/>
                <input type="hidden" name="contact_id[]" value="">
            </div>
        </div>
    </div>

    <additional></additional>
    <div id="panel2" class="panel panel-default magic-element isotope-item">
        <div class="panel-heading">
            <button type="button" class="btn btn-info btn-block" onclick="addRow(this.form)">Add</button>
        </div>
    </div>
</div>

JS

var rowNum = 0;
function addRow(frm) {
    rowNum++;
    var row = '<div id="panel2" class="panel panel-default magic-element isotope-item"><div class="panel-heading"><span class="panel-title">Additional</span></div><div class="panel-body"><div class="form-group"><input type="text" name="role[]" placeholder="Role" class="form-control" display="text" /> <input type="text" name="first_name[]" placeholder="First Name" class="form-control"  display="text"/><input type="text" name="last_name[]" placeholder="Last Name" class="form-control"  display="text"/><input type="text" name="phone[]" placeholder="Phone Number" class="form-control" display="text" /></div><div class="form-group"><input type="text" name="address_line_1[]" placeholder="Address Line 1" class="form-control"  display="text"/><input type="text" name="address_line_2[]" placeholder="Address Line 2" class="form-control"  display="text" value=""/><input type="text" name="line_3[]" placeholder="City, State Zip" class="form-control"  display="text"/><input type="hidden" name="contact_id[]" value=""></div></div></div>';
    $(row).insertAfter('additional');
}
¿Fue útil?

Solución

I think the click event of your button wasn't bound to your function addRow correctly.

Instead of binding the event from onclick="addRow(this.form)", try creating the click event as follows instead:

$(function() {
    $("#mybutton").click(function() {
        addRow($(this));
    });
});

where mybutton is your Add button:

<button type="button" class="btn btn-info btn-block" id="mybutton">Add</button>

Here's a Fiddle including the changes made: Fiddle

Have a try and let me know :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top