0

I am building a form using a js+html and I ran into a problem. There's a part of my form where user should be able to click on a textfield and pick a date & time from a calendar(anytime.js by MAM3), and since my form(partial code) is built this way:

third_list = "<table class='table'>";
if (radio_array[genIndex] == reserve) {
    third_list += "<tr><td id='Date:'><label><span id='Date'>Date:</span><input type='text' id='Date' name='_date' size='20' onfocus='showMessage()'/></label></td>";
    third_list += "<td id='Time:'><label><span id='Time'>Time:</span><input type='text' id='Time' name='_time' size='20' /></label></td>";
    document.getElementById("third").innerHTML = third_list;
    l3_value = "";
    return;
}

and by putting this to the html:

    <script type="text/javascript">
    AnyTime.widget
        ( "Date",
            { format: "%m/%d/%Z" }
        );
    AnyTime.widget
        ( "Time",
            { format: "%h:%i:%p" }
        );
    </script>

it does not pop-up a calendar.

Side notes: I have included all of the required js&css files and tried to see if it works on a seperate text field out from js, and it works. I think the reason it doesnt work is it is controlled by js, so the anytime.js does not see it as a html name field.

SN2: onfocus='showMessage()' in my js is to show a message when a user clicks on a text field.

How do I make it work?

makalele
  • 79
  • 1
  • 2
  • 10
  • 1
    Ids should be unique, three elements with the same are two too much. This also might be the root cause of your problem, although I don't know how anytime.js gets the elements. – Bergi Jun 20 '12 at 18:02
  • Thanks for the comment, I changed all of them and made them unique ids, issue still exists. any other suggestions by any chance? – makalele Jun 20 '12 at 18:21

1 Answers1

1

A couple of issues:

First, you have more than one element with the id values Date and Time. id values must be unique in the document. I expect the script isn't getting the element it expects and is failing to init. The documentation for AnyTime seems to suggest it uses the first argument you give AnyTime.widget as an id and expects to get an input field. In your case, it usually won't on most browsers, because when faced with an invalid structure featuring duplicate ids, most browsers will return the first one when you ask for "the" element with that id, which in your case is a span rather than an input field.

Your Date elements:

<span id='Date'>Date:</span><input type='text' id='Date' ... />
<!--      ^--- one                                 ^--- two -->

The Time is the same sort of problem.

Separately from that, I suspect you need to ensure that the elements exist when you call AnyTime.widget. I don't know where you have that script tag that calls it, but what you need to do is make this calls after you've executed the line

document.getElementById("third").innerHTML = third_list;

...so that the elements in question exist in the DOM. So for instance:

third_list = "<table class='table'>";
if (radio_array[genIndex] == reserve) {
    third_list += "<tr><td id='Date:'><label><span id='Date'>Date:</span><input type='text' id='Date' name='_date' size='20' onfocus='showMessage()'/></label></td>";
    third_list += "<td id='Time:'><label><span id='Time'>Time:</span><input type='text' id='Time' name='_time' size='20' /></label></td>";
    document.getElementById("third").innerHTML = third_list;
    l3_value = "";
    setUpWidgets();
    return;
}

// ...elsewhere in the same scope or a containing scope:
function setUpWidgets() {
    AnyTime.widget
        ( "Date",
            { format: "%m/%d/%Z" }
        );
    AnyTime.widget
        ( "Time",
            { format: "%h:%i:%p" }
        );
}

That creates a function, which you call when the elements are there.


Side note: You also have an id that looks like this:

 <td id='Date:'>

Although that's a valid id in HTML5, it wasn't valid in HTML4 and isn't valid in CSS. So if you try to use that id in a CSS-style selector (for instance, with jQuery), you'll probably run into trouble.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks for your quick reply. However, it did not solve the issue. I have changed them to totally different identifications and still not getting it. So duplicate id could be an issue but still does not resolve my problem. – makalele Jun 20 '12 at 18:17
  • @makalele: I've updated, I think you were also calling `AnyTime.widget` too early. – T.J. Crowder Jun 20 '12 at 18:21
  • I was actually calling anytime.widget before the closing tag of
    so,
    . I used your creating function method and also tried to put it as a onfocus='setUpWidgets()', no luck :(
    – makalele Jun 20 '12 at 18:41
  • I tried onfocus='setUpWidgets()' on a seperate textfield which was not in js, and it worked again. So, I believe there is an issue because of this **third_list +=** . – makalele Jun 20 '12 at 19:36
  • I am accepting your answer since it worked out! I used onfocus='setUpWidgets()' on one field, and it seemed to work for the other one automatically - magic! It was not working earlier - probably cached pages. Thank you very much! – makalele Jun 21 '12 at 04:36
  • Date and time fields appear after user chose any given radio button. After the first one, if a user changes his mind to choose a different radio button where new date and time fields appear, anytime widget will not appear on click. Any ideas? – makalele Jun 21 '12 at 04:56
  • If you haven't solved this yet: firstly, you're using an old version of anytime.js; you should download the latest (4.1112L) from www.ama3.com/anytime/ . Secondly, if you're using the same ID for the new input field that you used for the old input field, you'll need to call AnyTime.noPicker() on the ID before you call AnyTime.picker() a second time. The library keeps an internal table of IDs that have pickers on them, and refuses to create a second picker if a first picker already exists. Your JavaScript debugger/error console should have reported this as an exception. – Andrew M. Andrews III Jul 10 '12 at 01:31
  • BTW, if you have further problems using Any+Time(TM), try using the contact page on www.ama3.com as I will usually respond rather quickly. – Andrew M. Andrews III Jul 10 '12 at 01:32
  • @AndrewM.AndrewsIII: makalele won't have received notification of the comments above without an @ direction. (I did because I wrote the answer.) – T.J. Crowder Jul 10 '12 at 07:48
  • @makalele: See Andrew's comments above. – T.J. Crowder Jul 10 '12 at 07:49
  • Thanks Andrew and Crowder, I have solved the problem with both of your helps! Special thanks to AMA. – makalele Jul 14 '12 at 22:28