2

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

4
  • you can inline it with a <script> tag in your html. Commented Oct 12, 2017 at 6:53
  • <script> jQuery('select.alg-wselect').wSelect(); </script> in your html Commented 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.... Commented Oct 12, 2017 at 7:13
  • You'll probably have to load whatever jQuery-Plugin provides wSelect() before this. Commented Oct 12, 2017 at 7:18

1 Answer 1

2

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

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

8 Comments

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
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.
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 ! ! !
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.
Can you please share the url ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.