Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8467841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:45:44+00:00 2026-06-10T15:45:44+00:00

Possible Duplicate: Function triggering early calling a functions value I need some help. I

  • 0

Possible Duplicate:
Function triggering early
calling a functions value

I need some help.

I cannot, for the life of me, figure this out. I’ve had my head wrapped around it only to no avail.

I’d like to setup two functions.

  1. function: select_date() to interact with the user to select a date from the jQuery Date picker. If the dialog is closed then return as null.

  2. Then a second function: test() to check whether or not a date was picked/selected.

Here’s my dilemma, when the function test() is executed, an alert box pops up and says "undefined" which means, I never get to select a date and it always being "undefined"

I dont see what I am doing wrong here and everything seems logical to me.

<!DOCTYPE html>
<html>
<head>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.js"></script>

    <script type="text/javascript">

    function select_date() {
    var sdate

        $('#dd').dialog({
            autoOpen: true,
            modal: true,
            overlay: {
                opacity: 0.5,
                background: 'black'
            },
            title: "title",
            height: 265,
            width: 235,
            draggable: false,
            resizable: false
        });
        $('#d1').datepicker({
            onSelect: function () {
                $("#dd").dialog("close");
            }
        });
        return sdate
    }

    function test() {
    var x = select_date()

    alert(x)

    }

    </script>
    <style type="text/css">
    #d1 {font-size:64%;}
    </style>
</head>
<body>
<div id="dd">
<div id="d1">
</div>
</div>
<a href="javascript:test()">test</a>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:45:46+00:00Added an answer on June 10, 2026 at 3:45 pm

    It’s the way JavaScript works… select_date() returns immediately because .dialog() and .datepicker() are asynchronous methods. return sdate is called before anything has even happened. The right way to do it would be to use callback methods from onSelect()

    function select_date(callback) {
      $('#dd').dialog({
            autoOpen: true,
            modal: true,
            overlay: {
                opacity: 0.5,
                background: 'black'
            },
            title: "title",
            height: 265,
            width: 235,
            draggable: false,
            resizable: false
        });
        $('#d1').datepicker({
            onSelect: function () {
                $("#dd").dialog("close");
                callback($(this).datepicker('getDate'));
            }
        });
    }
    
    function test() {
      select_date(function(selected_date) { alert(selected_date); });
    }
    

    The only issue with this is that the callback will not be called if no date is selected and the dialog is closed. You could add an event listener for that on the dialog and make it call callback with a null/undefined value, but that’s up to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C++ virtual function from constructor Calling virtual functions inside constructors This question
Possible Duplicate: Function triggering early I have whipped up this code, but why on
Possible Duplicate: calling ASP function from javascript okay running this code : <script type=text/javascript>
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Faster DirectoryExists function? I want to check if some file exists on
Possible Duplicate: method vs function vs procedure vs class ? Can some one give
Possible Duplicate: The ultimate clean/secure function I was informed in another thread that this
Possible Duplicate: Function to Calculate Median in Sql Server Goal: Display the value of
Possible Duplicate: Caller function in PHP 5? Like this: function foo(){ do_something(); } function
Possible Duplicate: JavaScript function aliasing doesn't seem to work Why doesn't this work? function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.