1

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(){

}
});
6
  • how is process called? Commented Feb 17, 2014 at 8:16
  • Any idea what the function 'process' would do? Commented Feb 17, 2014 at 8:16
  • 1
    an easy solution is to read the values within the method like function process(){ var date = $('input[name="date"]').val(); } - else you can pass them as parameters Commented Feb 17, 2014 at 8:17
  • The problem is that date is input date which is changing to that is why there is .change. I tried Arun P Johnny solution but it does not work. Commented Feb 17, 2014 at 8:20
  • Function process is only example I only want to know how to get variable into this function. As I wrote before there is problem with changing that is why I can not use var date = $('input[name="date"]).val();.Can anyone help me with this? Thank you very much. Commented Feb 17, 2014 at 8:24

3 Answers 3

4

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.
}
});
Sign up to request clarification or add additional context in comments.

4 Comments

I tried use console.log in function process and console show this: event.returnValue is deprecated. Please use the standard event.preventDefault() instead.
This is a general error that will show in chrome console. You can safely ignore them. This has got nothing to do with your code. If you nedd to remove these warnings you can use strict mode.
Thank Makesh Sapkal add in your code process() and console.log is function. Thank you for your fast help.
Don't add comments in your answer; this post is fine just like it is.
1

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
    }
});

2 Comments

Perfect this solution work exactly as I need. Thank you and others for your help.
This is the same answer that Java_User gave 5 minutes earlier.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.