0

I have an Ajax call and response (see the code below.) Prior to this, I had regular javascript code that would successfully turn my select element from a traditional select to a select2 widget.

When I try to change the select into the select2 with the javascript code $('#datepicker').datepicker(); in the response from Ajax call, I get an error in the debugger that says

Uncaught TypeError: $(...).select2 is not a function at XMLHttpRequest.xhttp.onreadystatechange (createschedule.php:46)

Here is the code that is inside the javascript function:

var xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function() 
                {
                    if (this.readyState == 4 && this.status == 200) 
                    {
                        document.getElementById("row_"+cur_id).innerHTML = this.responseText;
                        $('#teacherselect').select2({ width: '100%' }); 
                        $('#datepicker').datepicker();
                    }
                };
                xhttp.open("GET", "editrow.php?id="+id, true);
                xhttp.send();

When Ajax returns and this code is executed, does it still have access to the items through jQuery? It appears that it does not.

7
  • When is this kicked off? Is the page fully loaded? Commented Jan 17, 2017 at 3:11
  • 1
    if you comment out that line, do you get an error on the datapicker line? Commented Jan 17, 2017 at 3:13
  • This is kicked off when a user presses a button (an image). @Bindrid Commented Jan 17, 2017 at 3:14
  • When I comment that out, the datepicker works successfully... that is really weird now. Commented Jan 17, 2017 at 3:15
  • 1
    so it sounds to me like your select 2 include is not loading, can you verify its path? Commented Jan 17, 2017 at 3:18

1 Answer 1

1

Because your jQuery and JQuery UI are working correctly, I would start checking the path of the select2. Is it local or a CDN?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I had two conflicting versions of jquery loading... one for the select2 and the other for the datepicker (jQuery UI)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.