Reputation: 97
I want to get the value of date in the text field. My HTML code:
<table id="sample">
<tr>
<td>
Date
</td>
</tr>
</table>
js file:
$("#sample tr td").after('<td><input type="text" id="datepicker"/></td>').queue(function() {
$('#datepicker').datepicker();
$(this).dequeue();
});
I want to alert the date which is given to the textbox. JSFiddle:
Upvotes: 0
Views: 791
Reputation: 386
try this... working for me!
$(document).ready(function(){
$(document).on("change","input#datepicker", function(){
if($(this).val()!="") {
alert($(this).val());
}
}); });
Upvotes: 0
Reputation: 870
$("#sample tr td").after('<td><input type="text" id="datepicker"/></td>');
$('#datepicker').datepicker({
onSelect: function(date) {
alert("You selected : " + date);
}
});
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<table id="sample">
<tr>
<td>
Date
</td>
</tr>
This will also work...
Upvotes: 0
Reputation: 870
$("#sample tr td").after('<td><input type="text" id="datepicker"/></td>').queue(function() {
$('#datepicker').datepicker({
onSelect: function(date) {
alert("You selected : " + date);
}
});
$(this).dequeue();
})
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<table id="sample">
<tr>
<td>
Date
</td>
</tr>
I did not quite get the question but I think you meant this!!
Upvotes: 0
Reputation: 3830
Alert the value on change like:
$(document).on("change","input", function(){
if($(this).val()!="") {
alert($(this).val());
}
});
Upvotes: 2
Reputation: 61
//In below code, you can replace input[type=text] with classname/id of input field.
$('input[type=text]').on('change', function() {
alert($('input[type=text]').val());
});
Upvotes: 1
Reputation: 388316
You can use .val() in any action with the selector to select the datepicker element to get its value.
Also not that the after() method is a synchronous method so there is no need to use queue()
$("#sample tr td").after('<td><input type="text" id="datepicker"/></td>');
$('#datepicker').datepicker();
$('button').click(function(){
alert($('#datepicker').val())
})
Demo: Fiddle
Upvotes: 1