Using jQueryExtern 3.0 library shows error js.JQuery is not a function

I am trying to use and test jQueryExtern 3.0 . The information/example available elsewhere seems not sufficient. I got one here:http://haxe.org/manual/target-javascript-external-libraries.html

import js.JQuery ;
new JQuery("#my-div").addClass(“brand-success”).html(“haxe is great!”);

So, in the index.html, I just created a div tag with id #my-div and used the above line of code in the Main class. The compilation is successful, but I get error in the browser console saying that js.JQuery is not a function.

What does that mean?

I read somewhere else that I need to create an Extern class as an interface? So, won’t above line of code alone will work?

Try changing that to import js.jquery.JQuery.

This extern (not extern class) comes with Haxe. There’s no need for you to create it.

Also, externs mostly just provide code completion. The actual JQuery functions are in JavaScript, and you can access them just fine without an extern. If you wanted, you could insert pure JavaScript code into your program:

untyped __js__('new jQuery("#my-div").addClass("brand-success").html("haxe+js is great!")');

That said, code completion is better than no code completion, so go ahead and use the extern.

Hmm… I tried again. But nothing seems to work.

I can see the entry

jQueryExtern: [3.0.0]

using command

haxelib list

on commandline, so it means that I successfully installed JQuery to be used. ( However you said, JQuery already comes with haxe, does that mean it can create a conflict? Should I not install it separately?)


After that I tried all these:

   import js.JQuery ; 
   untyped __js__( 'alert( new JQuery( "#some-div").text') ;

or
untyped __js__( 'alert( new js.JQuery( "#some-div").text') ;

For both of the above browser never displays alert box and the browser’s console says: JQuery is undefined


import js.jquery.JQuery ; 
untyped __js__( 'alert( new JQuery( "#some-div").text') ;

The above does not even compile. Haxe compiler says: Type not found js.jquery.JQuery


untyped __js__( 'alert( new js.jquery.JQuery( "#some-div").text') ;

Same as 1st. Browser never displays alert box and the browser’s console says: js.jquery is undefined

Thanks.

I think you have to download jquery and add it to your project (<dependency path="path/to/jquery-3.2.1.min.js" if="html5"/>). Then you can use built in externs.

Hmm… I will try. It is confusing, because they are not talking about the normal jquery library. But “Jquery Extern” which is installed as a library to haxe.

Externs provide code completion and nothing more (as far as I know). You have to import library separately.

I encountered the same error recently.
And the solution was really simple.

Just add this line in your build.hxml
-lib jQueryExtern

2 Likes