An issue occurred while attempting to replicate the element located within a specific section

I am having trouble cloning two label fields and their respective text fields after the div id/class that contains them when using a button. The cloned elements are currently being appended above, but I want them to appear below. I have tried using both the append and afterThis plugins from JQuery. Please take a look at this: Demo: JSFiddle

Any help would be greatly appreciated. Thank you!

$(function() {
$("#cloneadd").click(function() {
var clone ='<tr id="e00">'; 
clone +='<td colspan=1><label>Amount :</label></td>'; 
clone +='<td colspan=1><label>Detail :</label></td>' ;
clone +='</tr>';
clone +='<tr id="e01">';
clone +='<td colspan=1 id="opt_1"><input type="text" name="amt_exp" onkeypress="return isNum(event)" placeholder="Amount"/></td>';
clone +='<td colspan=1 id="opt_2"><input type="text" name="det_exp" placeholder="Detail"/></td>';
clone +='</tr>';
/*$("#expopt").append($(clone));*/
$(clone).insertAfter( "#expopt" );
});
});
<form action="xyz">
    <table>
        <tr>
            <td colspan=2>
                <label>Annual Amount :</label>
            </td>
        </tr>
        <tr>
            <td colspan=2>
                <input type="text" name="ann_amo" onkeypress="return isNum(event)" placeholder="Enter Amount" required />
            </td>
        </tr>
        <tr>
            <td colspan=2>
                <label>Info :</label>
            </td>
        </tr>
        <tr>
            <td colspan=2>
                <input type="text" name="ann_inf" placeholder="Enter Details" required />
            </td>
        </tr>
        <div id="expopt">
            <tr id="e00">
                <td colspan=1>
                    <label>Amount :</label>
                </td>
                <td colspan=1>
                    <label>Detail :</label>
                </td>
            </tr>
            <tr id="e01">
              <td colspan=1 id="opt_1">
               <input type="text" name="amt_exp" onkeypress="returnisNum(event)"/>
              </td>
              <td colspan=1 id="opt_2">
               <input type="text" name="det_exp" placeholder="Detail" />
              </td>
            </tr>
        </div>
        <tr>
            <td colspan=2>
                <input type="button" id="cloneadd" value="ADD"/>
            </td>
        </tr>
        <tr>
            <td colspan=2>
                <input type="submit" name="submit" value="Submit" />
            </td>
        </tr>
    </table>
</form>

Answer №1

Place it above the tr of the button

$(clone).insertBefore( $(this).closest('tr'));

Your html is not valid, a table cannot have a div as a child. To group tr elements, use tbody

<form action="xyz">
    <table>
        <tr>
            <td colspan=2>
                <label>Annual Amount :</label>
            </td>
        </tr>
        <tr>
            <td colspan=2>
                <input type="text" name="ann_amo" onkeypress="return isNum(event)" placeholder="Enter Amount" required />
            </td>
        </tr>
        <tr>
            <td colspan=2>
                <label>Info :</label>
            </td>
        </tr>
        <tr>
            <td colspan=2>
                <input type="text" name="ann_inf" placeholder="Enter Details" required />
            </td>
        </tr>
        <tbody id="expopt">
            <tr id="e00">
                <td colspan=1>
                    <label>Amount :</label>
                </td>
                <td colspan=1>
                    <label>Detail :</label>
                </td>
            </tr>
            <tr id="e01">
                <td colspan=1 id="opt_1">
                    <input type="text" name="amt_exp" onkeypress="returnisNum(event)" />
                </td>
                <td colspan=1 id="opt_2">
                    <input type="text" name="det_exp" placeholder="Detail" />
                </td>
            </tr>
        </tbody>
        <tr>
            <td colspan=2>
                <input type="button" id="cloneadd" value="ADD" />
            </td>
        </tr>
        <tr>
            <td colspan=2>
                <input type="submit" name="submit" value="Submit" />
            </td>
        </tr>
    </table>
</form>

then

$(function () {
    $("#cloneadd").click(function () {
        var clone = '<tr id="e00">';
        clone += '<td colspan=1><label>Amount :</label></td>';
        clone += '<td colspan=1><label>Detail :</label></td>';
        clone += '</tr>';
        clone += '<tr id="e01">';
        clone += '<td colspan=1 id="opt_1"><input type="text" name="amt_exp" onkeypress="return isNum(event)" placeholder="Amount"/></td>';
        clone += '<td colspan=1 id="opt_2"><input type="text" name="det_exp" placeholder="Detail"/></td>';
        clone += '</tr>';
        //or $(clone).appendTo('#expopt');
        $(clone).insertAfter('#expopt');
    });
});

Demo: Fiddle

Answer №2

Check out this file to assist you: http://jsfiddle.net/cpna3ba4/3/

$(function () {
    var counter = 1;
    $("#cloneadd").click(function () {
        counter = counter + 1;
        //$(clone).appendTo('#expopt');
        var clonTrHeading = $('#e00').clone().attr({'id':'e00'+counter});
        var clonTrInput = $('#e01').clone().attr({'id':'e01'+counter});  
        clonTrInput.find('input').val('');
        $('#expopt tr:last').after(clonTrHeading);
        $('#expopt tr:last').after(clonTrInput);
    });
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can I link the accordion title to a different webpage?

Is it possible to turn the title of an accordion into a button without it looking like a button? Here is an image of the accordion title and some accompanying data. I want to be able to click on the text in the title to navigate to another page. I am worki ...

Eliminate table styling within PHP

Is there a simple method to strip out inline styles and attributes from a table using PHP. For instance, how can I remove class, style, width, and height from the following code: <tr class=xl96 height=30 style='mso-height-source:userset;height:23 ...

Inter-class communication using TypeScript callbacks

Struggling with Typescript, I have encountered an issue while trying to send a callback from an angular controller to an angular service. Despite setting a break point at the beginning of the callback function using Chrome Dev Tools, it never gets triggere ...

What could be causing the PAGE CSS to malfunction?

I am looking to export HTML text as a word document with A4 size and portrait orientation. My JavaScript currently allows me to export the text, but it appears like a webpage format rather than in A4 or portrait mode. I have tried adding @page CSS styling ...

Bootstrap is an outdated page template that lacks responsiveness

My code below features a page designed using Bootstrap HTML and CSS, but it doesn't look right on all devices. For example, on mobile devices, the grid appears zoomed in and unresponsive. I tried removing grid-template-columns: auto auto;, which fixed ...

Updating Icons in HTML Using Local Storage and Implementing a Dark Mode Feature Using JavaScript

My latest project involved creating a dark mode button along with a custom dark theme. The theme is stored in Local Storage to ensure consistency. However, I have encountered an issue where, upon page reload, the site remains in dark mode but the button ic ...

Setting the minimum height of a wrapper to 100% may cause the

I am experiencing an issue with my website where the repeating background on a wrapper that acts as the background for the nav-bar is not extending all the way down the page as intended. I tried setting the min-height of the wrapper to 100%, but for some r ...

New and improved Bootstrap 5 menu bar

I am seeking to customize my navigation bar by adding a black background that spans its entire length. I do not want to rearrange the links or logo; just apply a background color. Another issue arises during SASS compilation: I am trying to change the col ...

Customizing the attribute of an HTML tag using EJS or jQuery

Within my express server, I am rendering a page with the following data: app.get('/people/:personID', function (req, res) { res.render("people/profile", {person: req.person }); }); Inside my profile.ejs file, I can display the data within an ...

Discover the art of effortlessly depositing elements into a spontaneously generated DIV, while luxuriating in the

Below is the jQuery code I am currently utilizing: jQuery(function() { jQuery(".component").draggable({ // utilize a helper-clone that will be appended to 'body' and not confined by a pane helper: function() { return jQuery(this ...

Can one manipulate the simulation to make isTrusted=true a reality?

Is there a way to simulate an isTrusted=true in touchStart event triggering? Are there any libraries or workarounds that can make this achievable? When I run the touchStart event programmatically versus physically triggering it, the output differs. Below ...

Angular2 - the pipe was not located while organizing records

I've successfully fetched data from an API and displayed it in the view, but I'm struggling to organize the data by date. Whenever I attempt to do so, I encounter this error message: The pipe 'groupBy' could not be found pipe.ts impor ...

Troubleshooting: Issues with executing a PHP script from jQuery

I found the source code on this website: It's an amazing resource, but I'm facing an issue where my JavaScript doesn't seem to be executing the PHP script... When I set a breakpoint in Chrome's debugger before the penultimate line (}) ...

Ensure that the placeholder remains visible as you type in the input field

Is there a way to implement an input text field in React with the placeholder "DD-MM-YYYY," that partially disappears as I start typing? For example, when I type "02-," only "-MM-YYYY" should remain visible as part of the placeholder. ...

The CSS selector for adjacent siblings seems to be malfunctioning

This code is designed to show the "container-1" division every time I hover over the "container" division. However, it's not behaving as expected because instead of displaying block on hover, it simply remains hidden. CSS Code .container:hover + .con ...

An issue has arisen regarding the width of the second image

Can you assist me in resolving an issue related to the width of the second image? I have included a link for your reference if you wish to examine the problem. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

Exploring ASP.NET Core 3.0: Animated Loading Icons

My latest project involves integrating a loading GIF, and I'm using ASP.NET Core 3.0 with Razor Pages for this task. It's been challenging for me as a newcomer to incorporate some Javascript or Ajax code to achieve the desired functionality. Upon ...

What is the JQuery method for disabling a button?

I'm currently working on a MVC application and I have a checkbox along with a submit button. The goal is to have the submit button enabled when the checkbox is checked, and disabled when it is unchecked. How can I achieve this? Here's the code ...

Conceal the overflow from absolutely positioned elements

I've gone through all the similar examples but still struggling to find the correct solution. My issue involves a basic Side Menu that toggles upon button click. Within the menu, there is a list of checkboxes positioned absolutely with the parent con ...

The event handler for clicking on the inner <li> element is not being triggered

I am encountering an issue in my React project, The problem lies within a menu structure that contains nested sub-menus. Here is the snippet of code: <ul id="main-menu-navigation" data-menu="menu-navigation" ...