views:

497

answers:

2

I'm creating a jquery time entry in an onready() handler and hiding it in another onready() handler in jquery. (The reason that the code is being called in two different onread() handlers is that the time entry is being created via a reusable Django widget renderer and hidden via local screen application logic.)

The jquery time entry gives a critical error if you try to create a jquery time entry on an input field that is hidden. The following code will cause fatal errors on IE7 and Firefox. If I hide() the entry after it is created everthing works fine, but I can't control the order of my two onready() handlers (this is due to framework issues that can't be worked around.) Is this a bug in the timeentry, or undefined behavior in browsers? Is there a graceful way to work around this without having a specific hack to work around the issue?

<html>
<head>
<script type="text/javascript" src="jquery_ui/jquery-1.3.js"></script>
<script type="text/javascript" src="jquery_ui/ui.core.js"></script>
<script type="text/javascript" src="jquery_time_entry/jquery.timeentry.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 $('#id_testTimePicker').hide();
});
$(document).ready(function(){
    var id = "#id_testTimePicker";
    $(id).timeEntry({spinnerImage: '%sjquery_time_entry/timeEntry.png'
    , timeSteps: [1, 5, 1]});
    $(id).timeEntry('setTime', new Date());
});
</script>
</head>
<body>
<input id="id_testTimePicker"></input>
</body>
</html>

UPDATE: In the above code it is actually only the "setTime" command that causes the browser error. If I change that to a $(id).setVal(str) where str has the proper time format setup for the widget then everything works fine.

A: 

Can't do this?

$(document).ready(function(){
    var id = "#id_testTimePicker";
    $(id).timeEntry({spinnerImage: '%sjquery_time_entry/timeEntry.png'
    , timeSteps: [1, 5, 1]});
    $(id).timeEntry('setTime', new Date());
    $(id).hide();
});
Ironicnet
+1  A: 

In the second ready handler, include a call to ".show()" before calling timeEntry, then ".hide()" it again afterwards.

That said, it seems odd that timeEntry would care if it was visible or not. It really shouldn't matter, so I would guess it's a bug in timeEntry.

EDIT: If you don't know in the second handler if it should be hidden or not, just query the state of the element before hand with "$(id).is(':hidden')" and behave accordingly.

nezroy
This actually doesn't fix the problem in my case because the time entry may still not be visible because its parent containers may be hidden. The time entry is trying to set focus when it has a value set on it.
MikeN