Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conflict with two jquery datepickers on same page

Tags:

So here is the setup: I have two jquery datepickers, inside a jquery tab, inside a jquery modal dialog window:

\---/\---/\---/_______________ /                            / \                            \ /  DATEPICKER1               / \                            \ /  DATEPICKER2               / \                            \ /                            / \                            \ /____________________________/ 

The first datepicker functions normally, but when I try to click a date in the second datepicker it simply activates the first one. Did you follow that? :)

So to sum up, clicking a date in datepicker2 activates datepicker1.

I have no idea why this is happening - they have different ids and names, as outlined below.

To create the datepickers I'm just using:

$(function() {     $("#datepicker1").datepicker();     $("#datepicker2").datepicker(); }); 

The fields are simply:

<input type="text" id="datepicker1" name="datepicker1" value="" /> <input type="text" id="datepicker2" name="datepicker2" value="" /> 

I'm using jQuery v1.9.0 and jQueryui v1.10.0.

Any thoughts on this? As a caveat, I am unable to post actual code due to restrictions from my employer, but I can answer most questions if you need any clarification. Any and all help would be greatly appreciated!!

like image 578
musicmunky Avatar asked Apr 23 '13 12:04

musicmunky


People also ask

How do I use two Datepickers on the same page?

Just adding a textbox with datepicker id will not work properly, you need separate id or class with separate jQuery functions. Where two jQuery datepicker functions are used, ie, first datepicker is for text input with id “datepicker1” and second datepicker for text input with id “datepicker2”.


1 Answers

UPDATE: It looks like the behavior described below was addressed in another stackoverflow question here:
Prevent jQuery UI dialog from setting focus to first textbox

Apologies for the "duplicate" question - if I had known this was the problem I would have figured it out quickly!!!

################################################################################

Well, the resolution is very simple, and perhaps someone can enlighten me as to why it works this way because I'm a bit clueless at the moment.

Here's what I did:
I added another two text input fields to the form (they were needed) and reordered the layout so that one of those new input fields was the "first" element (in the top-left corner), like so:

\---/\---/\---/_______________ /                            / \                            \ /  TEXTFIELD1   DATEPICKER1  / \                            \ /  TEXTFIELD2   DATEPICKER2  / \                            \ /                            / \                            \ /____________________________/ 

Suddenly the problem has vanished! However, I notice an interesting behavior:
When I select a date in EITHER datepicker, the cursor then immediately jumps back to the first text field. So if this were happening when I had the datepickers with no text fields, that behavior would mean that the cursor would immediately have jumped back to the first datepicker which could have cause the issue I was having.

Now, as to WHY it works this way I have no idea. I tried setting the tabindex parameters for the various textfields/datepickers but that didn't change the behavior.

Anyway, I appreciate the input from everyone who chimed in - this was a weird problem, but I'll never forget how to fix it now. Thanks everyone for your help!!

like image 98
musicmunky Avatar answered Nov 04 '22 09:11

musicmunky