/    Sign up×
Community /Pin to ProfileBookmark

Jquery not working on dynamically created nodes

Hey Guys,

Heres the issue I have. I’m unable to apply an event to a newly created input box. See below for the HTML.

[code=html]
<table style=”border:#666 solid 2px; border-collapse:collapse;” border=”2″ bordercolor=”#666″ width=”100%” id=”action”>
<thead>
<tr>
<td class=”tdtblf” width=”150px”><strong>Date / Time</strong></td>
<td class=”tdtblf” width=”240px”><strong>By Who</strong></td>
<td class=”tdtblf”><strong>Action</strong></td>
</tr>
</thead>
<tbody>
<tr>
<td><input type=”text” id=”caldatetime” class=”caldatetime” value=”” /></td>
<td>John Doe</td>
<td><input type=”text” style=”width:450px” value=”” id=”actiondata” /></td>
</tr>
</tbody>
</table><br />
<p><div id=”add_row”><img src=”/img/create.gif” style=”margin-top:-2px; float:left” /> Add another entry</div></p>
[/code]

jQuery lib is 1.7.1

Jquery:

[CODE]
//jQuery for Box Add Actions

$(‘#add_row’).click(function(){

if( $(‘#action tr:last-child input#caldatetime’).val().length > 0 && $(‘#action tr:last-child input#actiondata’).val().length > 0 ) {
$(‘#action tr:last-child input.caldatetime’).attr(“disabled”,”disabled”);
$(‘#action tr:last-child input#actiondata’).attr(“readonly”,”readonly”);
$(‘#action tr:last-child input.caldatetime’).removeClass(“caldatetime hasDatepicker”);
$(‘#action’).append(‘<tr><td><input type=”text” class=”caldatetime” id=”caldatetime” value=”” /></td><td>John Doe</td><td><input type=”text” id=”actiondata” style=”width:450px” /></td></tr>’);

}

});

//Calendar Callers
// And
//Function for appending fields
$(“.caldatetime”).live(“click”, function(){
$(this).datetimepicker({
dateFormat: ‘yy-mm-dd’,
timeFormat: ‘hh:mm:ss’,
stepHour: 1,
stepMinute: 2
});
});
[/CODE]

Whats happening is when I create a new node I wish to click in the date box and bring up the datetime calendar picker I have placed on the page. It add the date thing to the first preset node correctly and I can add a new row(contains another input) and even click that second box and bring up the calendar fine. How ever when I select the box it changes the value in the first box only…

I have a feeling it to do with a selector I’m using. Any ideas?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@ericatekkaauthorFeb 24.2012 — I had to remove the class which has a different name to the HTML as the jquery runs on document ready and adds another class name to the fields it'll be firing on.
Copy linkTweet thisAlerts:
@ericatekkaauthorFeb 24.2012 — Changed the jQuery a bit

Still no good. Pulls up the calendar quicker tho.
[CODE]
//jQuery for Box Add Actions

$('#add_row').click(function(){

if( $('#action tr:last-child input#caldatetime').val().length > 0 && $('#action tr:last-child input#actiondata').val().length > 0 ) {
$('#action tr:last-child input.caldatetime').attr("disabled","disabled");
$('#action tr:last-child input#actiondata').attr("readonly","readonly");
$('#action tr:last-child input.caldatetime').removeClass("hasDatepicker");
$('#action').append('<tr><td><input type="text" class="caldatetime" id="caldatetime" value="" /></td><td>John Doe</td><td><input type="text" id="actiondata" style="width:450px" /></td></tr>');
$('.caldatetime:last').datetimepicker({dateFormat: 'yy-mm-dd',
timeFormat: 'hh:mm:ss',
stepHour: 1,
stepMinute: 2
});
}

});

[/CODE]
Copy linkTweet thisAlerts:
@ericatekkaauthorFeb 24.2012 — code is
[CODE]
$('#add_row').click(function(){

if( $('#action input.caldatetime:last').val().length > 0 && $('#action input.actiondata:last').val().length > 0 ) {
var count = $('.caldatetime').size();
$('#action tr:last-child input.caldatetime').attr("disabled","disabled");
$('#action tr:last-child input#actiondata').attr("readonly","readonly");
$('#action').append('<tr><td><input type="text" class="caldatetime" id="caldatetime'+count+'" value="" /></td><td>John Doe</td><td><input type="text" id="actiondata" class="actiondata" style="width:450px" /></td></tr>');
$('#caldatetime'+count).datetimepicker({dateFormat: 'yy-mm-dd',
timeFormat: 'hh:mm:ss',
stepHour: 1,
stepMinute: 2
});
}

[/CODE]
×

Success!

Help @ericatekka spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...