After clicking the 'add row' button, I successfully added a new row; however, the jQuery date

I encountered an issue with the datepicker when adding it to dynamically added rows. The datepicker was not functioning properly on the added rows, except for the first row where it worked perfectly. Strangely, removing the first row resulted in the datepicker working correctly on the remaining added rows.

Below is the code snippet:

$(function() {
    $( ".datepicker" ).datepicker({

         defaultDate: "+1w",
         changeMonth: true,
         maxDate: new Date(), 
         dateFormat: 'yy-mm-dd',
         numberOfMonths: 1,

    });  });

function addRow(tableID) {
        var table       = document.getElementById(tableID);
        var rowCount    = table.rows.length;
        var row         = table.insertRow(rowCount);
        var colCount    = table.rows[0].cells.length;

        for(var i=0; i<colCount; i++) {

            var newcell = row.insertCell(i);
            newcell.innerHTML = table.rows[0].cells[i].innerHTML;
            switch(newcell.childNodes[0].type) {
                case "text":
                        newcell.childNodes[0].value = "";
                        break;
                case "checkbox":
                        newcell.childNodes[0].checked = false;
                        break;
                case "select-one":
                        newcell.childNodes[0].selectedIndex = 0;
                        break;
            }
        }
        var $row = $(row);
        $row.find('input.datepicker').datepicker({
             maxDate: new Date(), 
             dateFormat: 'yy-mm-dd',
        });

    }

Here is the HTML code corresponding to the JavaScript above:

<input type="button" class="btn btn-default" value="Add Row" onClick="addRow('add_row')"/>
 <table class="table table-bordered" id="adddeleterow">

                                <tbody id="add_row">
                                <tr>
                                <td><input class="form-control datepicker" type="text" name="date[]" id="date" placeholder="Date"/>
                                    </td>
                                    <td>
                                        <input class="form-control" type="text" name="hours[]" id="hours" placeholder="hours(hh:mm)"/>
                                    </td>
                                    <td><input type="button" class="btn btn-default" id="delPOIbutton" value="Delete" onClick="delete_check('add_row',this)" /></td>
                                    </tr>
                                </tbody>
                            </table>

Answer №1

The issue stemmed from the second line of code.

This is what you currently have:

$( ".datepicker" ).datepicker({

It should be revised to this:

$(function() {
$(".datepicker").datepicker()({

  defaultDate: "+1w",
  changeMonth: true,
  maxDate: new Date(),
  dateFormat: 'yy-mm-dd',
  numberOfMonths: 1,

}); });

Furthermore, you must bind a click event to addRow and reposition the newly added row within the document.

$(".datepick").datepicker();
var newRowNum = 0;

// attach a click event handler to the "Add" button
$('#addnew').click(function() {

$(".datepick").datepicker("destroy");

For further information, please visit: http://jsfiddle.net/norcaljohnny/hq2r9Lbq/

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

PHP does not provide any error when an update on a database row fails

When I try to update values retrieved from the database into a form on submission, the process fails without any error message. Below is the PHP code snippet: <?php session_start(); $username=$_SESSION['uname']; $cn=mysqli_connect("loc ...

Is your JQuery Gallery experiencing issues with the next button function?

I'm working on developing a simple gallery using JQuery. The main concept is to have all image files named x.png (where x is a number), and the program will then add a number to the current one, creating x+1.png and so forth. Here's the code I ...

Issue with Ajax when attempting to load new content into a div after submitting a form

After implementing the code below to submit a form and use ajax to load the action page into a div on the same page, I am encountering an issue where clicking submit results in a duplicate copy of the form appearing again. <script src="//ajax.googleapi ...

jQuery: Set default option selected or reset value

I'm attempting to change the value of the nearest select option dynamically. $(document).ready(function () { $('.limitation_points').hide(); $('.field .limitSelected').each(function () { $(this).change(function ( ...

Move the absolute div by sliding it to the left from 120% to -10px

I want to move an absolute positioned div from the left side of the screen to -10px on button click. Here's my attempt so far, but it's not working as expected. Javascript/jQuery $('.button').click(function() { $(this).parent().f ...

Add space between the first and second rows in the grid gallery display

.gallery{ display: grid; grid-template-columns: repeat( auto-fit, minmax(250px, 1fr) ); grid-template-rows: repeat( auto-fit, minmax(250px, 1fr) ); } view grid image here Could you assist me with identifying the spacing issue between the first and second ...

Enhancing Visuals with src="imageUrl within Html"

Is there a way to customize the appearance of images that are fetched from an API imageUrl? I would like to create some space between the columns but when the images are displayed on a medium to small screen, they appear too close together with no spacing. ...

Artwork expanding incorrectly on HTML canvas

I'm encountering an issue while attempting to draw on an HTML canvas. I've specified 50 circles and multiple lines within a canvas of size 1000x1000 px, but not all circles are appearing as expected. My assumption is that the elements are being ...

What is the best way to remove the hover effect from a specific element within a div?

I am looking to achieve a specific hover effect where the white part does not darken when hovering over a certain element within its child elements. Here is the HTML code I have: <div className= {css.searchBarDiv}> <div className={css.searchBar ...

Overflow of content within a rectangular element

One challenge I'm facing is creating HTML/CSS code that automatically adjusts the website layout (blocks and content) when I resize the browser window. For example: I encounter text overflow issues in a block I've created when I minimize the brow ...

Insert information into the <div> element

Sorry if this is a repeated question, but I'm struggling to locate the solution. Is there a way to use jQuery to add content within a div, depending on its current class? For instance, adding "CODE" to a div such as: <div "CODE" class="class_one" ...

Sending Django Variable With Javascript

As a newcomer to Javascript, I am grappling with retrieving the value of a variable from my HTML form. My current code seems to be somewhat functional - I added an alert to test the logic and found that the else statement is working fine. However, I'm ...

Is there a way for the React select component to adjust its width automatically based on the label size?

After defining a React select component, it looks like this: <FormControl> <InputLabel id="demo-simple-select-label">Age</InputLabel> <Select labelId="demo-simple-select-label" id=&quo ...

Using JavaScript variables in a Jinja array

I encountered a frustrating issue that has been causing me some trouble. In my project setup, data is being transferred from Python to the frontend using Jinja in the form of a 2D list. My goal is to loop through this array using a for loop, but I'm ...

Anchor elements and other inline elements not triggering MouseLeave event

Take a look at this CodePen link, particularly in Chrome: https://codepen.io/pkroupoderov/pen/jdRowv Sometimes, the mouseLeave event doesn't trigger when a user quickly moves the mouse over multiple images. This results in some images still having t ...

What's the reason for text-alignment not functioning properly?

After some testing, I have come up with the following code: .Greetings { width:100%; display:block; text-align:center; font-family: "Times New Roman", Times, serif; font-size:75px; color:#208CB7; } The objective was to center the text on the ...

Display a block by using the focus() method

My objective is : To display a popin when the user clicks on a button To hide the popin when the user clicks elsewhere I previously implemented this solution successfully: Use jQuery to hide a DIV when the user clicks outside of it However, I wanted to ...

Tips for creating a responsive H1 tag within the Avada WordPress theme by implementing CSS

One of my acquaintances recently upgraded his Avada WordPress theme to the latest version of WordPress without physically uploading any files. However, after this upgrade, he noticed that the page titles (H1) no longer appear responsive as they did before. ...

When viewing the material-ui Chip component at normal zoom, a border outlines the element, but this border disappears when zoomed in or out, regardless of

Edit I have recently discovered a solution to the unusual problem I was facing with the material-ui Chip Component. By adding the line -webkit-appearance: none; to the root div for the Chip, the issue seems to resolve itself. However, this line is being a ...

I'm feeling pretty lost when it comes to understanding how line-height and margins work together

When creating a webpage layout, my goal is to maintain balance by ensuring there is an equal amount of spacing between sections and elements. However, when dealing with varying font sizes and line heights, achieving this can be challenging. To provide fur ...