最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jquery ui datepicker not working with dynamic html - Stack Overflow

matteradmin6PV0评论

I have successfully attached jquery UI datePicker to a dynamically created textbox, Al works fine excepts when I select a date it is not ing in corresponding textbox, But it es in my first textbox which is not dynamically created. My code is like this

 $(".add-more").click(function () {
        $this = $(this);
        $section = $($("." + $this.attr("data-section"))[0]).html();// This is a section in my HTML with multiple textboxes
        $this.parent().append('<div class=' + $this.attr("data-section") + '>' + $section + '</div>').fadeIn(); 

        $(".datepicker").removeClass('hasDatepicker').datepicker();

    });

HTML

<div id="Div1" class="section-container">
    <div class="education-item" style="display: none">
        <p>Institute:<input type="text" name="txtInstitute"   /></p>
        <p>Start Year:<input type="text" name="txtStartYear" class="datepicker" /></p>
        <p>End Year:<input type="text" name="txtEndYear"  class="datepicker"/></p>
    </div>
</div>

JSFiddle Here

I have successfully attached jquery UI datePicker to a dynamically created textbox, Al works fine excepts when I select a date it is not ing in corresponding textbox, But it es in my first textbox which is not dynamically created. My code is like this

 $(".add-more").click(function () {
        $this = $(this);
        $section = $($("." + $this.attr("data-section"))[0]).html();// This is a section in my HTML with multiple textboxes
        $this.parent().append('<div class=' + $this.attr("data-section") + '>' + $section + '</div>').fadeIn(); 

        $(".datepicker").removeClass('hasDatepicker').datepicker();

    });

HTML

<div id="Div1" class="section-container">
    <div class="education-item" style="display: none">
        <p>Institute:<input type="text" name="txtInstitute"   /></p>
        <p>Start Year:<input type="text" name="txtStartYear" class="datepicker" /></p>
        <p>End Year:<input type="text" name="txtEndYear"  class="datepicker"/></p>
    </div>
</div>

JSFiddle Here

Share Improve this question asked Mar 15, 2014 at 10:48 NoneNone 5,68023 gold badges93 silver badges178 bronze badges 2
  • Try jsfiddle/xUYM2 ( i just use more accurate indication of elements to create datepicker ) – Vasiliy vvscode Vanchuk Commented Mar 15, 2014 at 11:33
  • Ahhh..That work like a charm. You can add this as answer here :) – None Commented Mar 15, 2014 at 11:38
Add a ment  | 

2 Answers 2

Reset to default 5

Try to use more accurate indication of elements to create datepicker. Like here http://jsfiddle/xUYM2/

$(".add-more").click(function() {
    $this = $(this);
    $section = $($("." + $this.attr("data-section"))[0]).html();
    var block = $('<div class=' + $this.attr("data-section") + '>' + $section + '</div>')
    $this.parent().append(block).fadeIn();

    block.find(".datepicker").removeClass('hasDatepicker').datepicker();
    showHideRemove($(this).parents(".section-container").find(".remove-item"), 1);
});

P.S> it will be better to use local variables in you script like

    var $this = $(this); // without var keyword your variable will be global
    var $section = $($("." + $this.attr("data-section"))[0]).html();

Add this line in the add-more function

$(".datepicker").removeClass('hasDatepicker').datepicker(); 
Post a comment

comment list (0)

  1. No comments so far