I have problem with jquery. I need to use variable date and time in function process but I do not how. Please can you anyone help me. Thank you.
$(document).ready(function () {
$('input[name="date"]').change(function(){
var date = $('input[name="date"]').val();
document.getElementById("date").innerHTML = date;
});
$('input[name="time"]').change(function(){
var time = $('input[name="time"]').val();
});
function process(){
}
});
3 Answers 3
Try this:
$(document).ready(function () {
var date; //declared global
var time;//declared global
$('input[name="date"]').change(function(){
date = $('input[name="date"]').val();
document.getElementById("date").innerHTML = date;
});
$('input[name="time"]').change(function(){
time = $('input[name="time"]').val();
});
function process(){
//use date, time var as needed.
}
});
4 Comments
strict mode.Hope my understanding is correct i.e. you want to call function process() everytime when the date/time changes.
$(document).ready(function () {
var date, time; // declare them on top
$('input[name="date"]').change(function(){
date = $('input[name="date"]').val();
document.getElementById("date").innerHTML = date;
process();
});
$('input[name="time"]').change(function(){
time = $('input[name="time"]').val();
process();
});
function process(){
// use date, time here
}
});
I know two answers have been given already, but here's another approach you could adopt; cache the selectors for date and time fields, and use those within the context of process():
jQuery(function($) {
var $date = $('input[name="date"]'),
$time = $('input[name="time"]');
function process()
{
// use $date.val() and $time.val()
}
$date.on('change', function() {
$('#date').text(date);
process();
});
$time.on('change', process);
});
The advantage is that you don't start with an undefined date or time variable, because they're already references to their corresponding fields on the page.
processcalled?function process(){ var date = $('input[name="date"]').val(); }- else you can pass them as parameters