juice juice - 4 months ago 6x
jQuery Question

Getting a typeerror function does not exist error

Things to note:

I don't have access to HTML (rendered by an engine)

jQuery is included in the engine (which is why I don't include it in my code)

This code takes a link and injects it into the html:


function addScript(str){
var script = document.createElement('script');
script.src = str;

function addSS(str){
var link = document.createElement('link');
link.rel= 'stylesheet';
link.href = str;
link.type= 'text/css';

InjectedHTML puts datepicker into HTML (in a separate file from the code above):

$(document).ready(function() {
addClass(); //adds class attribute to an input that allows me to use datepicker function
datePicker(); //adds calendar
function addClass(){
$("#DateWrapper input").attr("class", "datepicker");

function datePicker(){
$( ".datepicker" ).datepicker();

Getting this error:

InjectedHTML.js:35 Uncaught TypeError: $(...).datepicker is not a function

Thanks for your help!


Its been a few years since I've looked at the state of manually loading scripts into the DOM, but there is usually a variant of a loaded event that fires when the script has actually finished loading.

Generally there is something asyncronous about the process, because it has to go and fetch the file. This means they're likely not going to stop the execution of the javascript engine while this is happening. This means in some form of fashion you're looking at a callback.

There are various libraries for doing this. If you don't want to use them, go read the source and investigate the requirements of the platform you're targeting.

Stuff like this:

Looks like jquery even has some form of this: https://api.jquery.com/jquery.getscript/