2

I am trying to use the jQuery datepicker for a form on a site that uses jQuery 1.3.2 but it's not working. I have to reference a newer jQuery library for some of my form functionality, and also the jQuery ui for datepicker to work. I have used noConflict for the newer jquery library but it's still not working. I get Uncaught TypeError: Cannot read property 'document' of null in the console. Updating/removing the 1.3.2 reference is not an option.

Here is my fiddle, which works. but i get the error above in Chrome (not FF) and the datepicker will not work on my site. http://jsfiddle.net/pnA33/

Can anyone help? It works locally but not on the server (which is a dev enviornment so I can't share a link). I found this but as I am relatively new to jQuery it is over my head.

jQuery:

            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
            <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
            <script type="text/javascript">
            var jQuery_1_9_1 = jQuery.noConflict(true);
            jQuery_1_9_1(document).ready(function() {
            jQuery_1_9_1( "#datepicker" ).datepicker({ minDate: -1, maxDate: "+24D" });
            jQuery_1_9_1('#pancettaForm').change(function () {
                       jQuery_1_9_1('.address,#new-ship-date').hide();
                       if (jQuery_1_9_1('#ship-address').prop('checked')) {
                          jQuery_1_9_1('.address').show();
                       }
                       else if (jQuery_1_9_1('#ship-date').prop('checked')) {
                          jQuery_1_9_1('#new-ship-date').show();
                       }
                       else if (jQuery_1_9_1('#ship-both').prop('checked')) {
                          jQuery_1_9_1('.address, #new-ship-date').show();
                       }
                    });

            });

            function validateForm()
            {
            var x=document.forms["pancettaForm"]["order-number"].value;
            if (x==null || x=="")
            {
            alert("Please provide your order number from the confirmation email sent immediately after placing your order.");
            return false;
            }

            }
            </script> 

HTML:

        <form name="pancettaForm" method="post" action="http://lizlantz.com/lcform.php" id="pancettaForm" onsubmit="return validateForm()">
                <input type="hidden" value="Pancetta Order Update" name="subject"> 
                <input type="hidden" value="cookware/partners_10151_-1_20002" name="redirect">
                <ol>
                    <li>
                    <label for="update-ship">I'd like to:</label> 
                        <input id="ship-address" name="update-ship" type="radio" value="update-ship-address"/> Have pancetta shipped to a different address than my skillet<br />
                        <input id="ship-date" name="update-ship" type="radio" value="update-ship-date" /> Have pancetta shipped sooner than June 14, 2013 <br />
                        <input id="ship-both" name="update-ship" type="radio" value="update-both" /> Make changes to both the shipping address and shipping date
                    </li>
                    <li>                
                    <label for="order-number"><em>*</em>Order Number (available in order confirmation email):</label> 
                        <input type="text" name="order-number">
                    </li>             
                    <li>                
                    <label for="full-name"><em>*</em>Recipient Full Name:</label> 
                        <input type="text" name="full-name">
                    </li>   
                    <li class="address" style="display: none;">
                        <label for="address">
                            <em>*</em>Address
                        </label> 
                        <input type="text" name="address">
                        <label for="address2">
                            Address Line 2
                        </label> 
                        <input type="text" name="address2">
                    </li>
                    <li class="address" style="display: none;">
                        <label for="city">
                            <em>*</em>City
                        </label> 
                        <input type="text" name="city">
                        <label for="state">
                            <em>*</em>State
                        </label> 
                        <select name="state">
                            <option value="AL">Alabama</option>
                            <option value="AK">Alaska</option>
                        </select>
                        <label for="zip">
                            <em>*</em>Zip Code
                        </label> 
                        <input type="text" name="zip">
                    </li>
                    <li id="new-ship-date"  style="display: none;">
                        <em>*</em><label for="updated-ship-date">New Ship Date:</label>
                        <input type="text" id="datepicker" name="updated-ship-date" value="Update Your Ship Date" />
                    </li>            
                    <li>
                        <label for="phone">
                            <em>*</em>Phone (for delivery questions)
                        </label> 
                        <input type="text" name="phone">
                    </li>               
                </ol>
                       <input type="submit" id="button" name="submit"  value="Update Pancetta">

              </form>
Community
  • 1
  • 1
surfbird0713
  • 1,209
  • 2
  • 23
  • 45
  • any way you could create a jsfiddle for this? – Jay Rizzi May 01 '13 at 20:31
  • 1
    Don't include two versions of jQuery in the same page. That's just begging for problems. – j08691 May 01 '13 at 20:32
  • the error doesnt make sense in this context, you are using noConflict correctly...we need to see more code. and while I totally agree with @j08691 that you are better off using one library (most are backwards compatible...esp with JQUI), you shouldn't be receiving this error. – Mike H. May 01 '13 at 20:32
  • I can't upgrade or modify the reference to the 132 library on this site, so I have no choice but to use 2. – surfbird0713 May 01 '13 at 20:53
  • @JayRizzi created fiddle, updated post. – surfbird0713 May 01 '13 at 20:53

1 Answers1

1

Ah ha! You actually cannot run two versions of jQuery on the same document instance at one time...hence the issue here. This solution comes directly from Splendìd Angst's answer in this thread Can I use multiple versions of jQuery on the same page?

Basically you must declare your noConflict version prior to entering the document "main loop" I suppose you can call it.

Declare your noConflict variable outside the document.ready call...use the standard $(document).ready() syntax, then use (and ONLY use) your noconflict variable within the ready closure. That should do it.

I haven't tested this mind you but it makes sense and it won't take you much tweaking of your code to try it out.

TIL something new about jQuery :)

Community
  • 1
  • 1
Mike H.
  • 1,731
  • 9
  • 31
  • Thanks for directing me to that, I followed Spelndid Angst's steps. Everything still works locally but not when I upload it to the server. Does noConflict address the jQueryUI conflict or just conflicts between jQuery libraries? Maybe it's the same thing... – surfbird0713 May 02 '13 at 12:50
  • 1
    Well, it shouldn't be a problem...especially if it runs locally. Ask yourself what is different about your local and live environments. Also, what is the exact reason you are trying to use two different libraries? There has to be a really really good one (like using a plugin/extension that is only supported for a particular library...which is rare). – Mike H. May 02 '13 at 14:02