I have a small javascript file (alg-wSelect.js) which has only one line of code: jQuery('select.alg-wselect').wSelect(); It's called by a wordpress plugin. Is it possible to add it on another javascript file or include it inline? How can I do that? Thank you in advance
-
you can inline it with a <script> tag in your html.falloutx– falloutx2017-10-12 06:53:58 +00:00Commented Oct 12, 2017 at 6:53
-
<script> jQuery('select.alg-wselect').wSelect(); </script> in your htmlfalloutx– falloutx2017-10-12 06:55:05 +00:00Commented Oct 12, 2017 at 6:55
-
Thanks for your reply! ! I tried that but I get Uncaught TypeError: jQuery(...).wSelect is not a function at (index):3054 ! ! ! How could I include / add this : jQuery('select.alg-wselect').wSelect(); into another .js file? ie I have : wSelect.min.js file. How can I add the jQuery('select.alg-wselect').wSelect(); Show I put it on a new line???? I don't know....its4yougr– its4yougr2017-10-12 07:13:54 +00:00Commented Oct 12, 2017 at 7:13
-
You'll probably have to load whatever jQuery-Plugin provides wSelect() before this.janh– janh2017-10-12 07:18:38 +00:00Commented Oct 12, 2017 at 7:18
Add a comment
|
1 Answer
This can be done using wp_add_inline_script. Please find the code below :
wp_enqueue_script( 'handel', 'JS FILE PATH' , array(), 'version', true );
$integrate_script="jQuery('select.alg-wselect').wSelect()";
wp_add_inline_script( 'handel', esc_js($integrate_script) );
wp_add_inline_script it will add script inline to the previously enqueue script. Please read about the function from here
8 Comments
its4yougr
Thanks for replying......I added this into functions.php: wp_enqueue_script( 'handel', '/wp-content/plugins/currency-switcher-woocommerce/includes/js/alg-wSelect.js' , array(), 'version', true ); $integrate_script="jQuery('select.alg-wselect').wSelect()"; wp_add_inline_script( 'handel', esc_js($integrate_script) ); but I get : Uncaught SyntaxError: Invalid or unexpected token jquery-migrate.min.js:1 JQMIGRATE: Migrate is installed, version 1.4.1 alg-wSelect.js:1 Uncaught TypeError: jQuery(...).wSelect is not a function at alg-wSelect.js:1
Tristup
I think you need to read the doc for this function first. You should know what is "handle" and what is "version" is referred. As you have explained that you need to add the small script that is in the file "alg-wSelect.js". So you dont need to enqueue that, you need to add the code using inline js function with any js file which has already enqueued.
its4yougr
I changed the code into the functions.php file to : wp_enqueue_script( 'handel', '/wp-content/plugins/currency-switcher-woocommerce/includes/lib/wSelect/wSelect.min.js' , array(), 'version', true ); $integrate_script="jQuery('select.alg-wselect').wSelect()"; wp_add_inline_script( 'handel', esc_js($integrate_script) ); But I still get : Uncaught SyntaxError: Invalid or unexpected token.... The file : wSelect.min.js is already enqued ! ! !
Tristup
If you have already enqueued a file you just have to use the "handle" as in my code. Please read it carefully, "handle" is a text which used in eneueue function while enqueuing the file, you need to use that "handle" text while adding the "inline" function.
Tristup
Can you please share the url ?
|