Super Sale Limited Time 50% OFF for All-Access Plans
Save 50% Now

Laravel: select2 in single page not work


I am using this code.


"use strict";

// Class definition
let KTSearch = function () {
// Private variables
let form;

// Init Select2
const initSelectXYZ = () => {

// Init Select2 --- more info:
$("[name="provincia_id"]").select2();
}

// Public methods
return {
init: function () {
form = document.getElementById("kt_search_form");
initSelectXYZ();
}
};
}();

// On document ready
KTUtil.onDOMContentLoaded(function () {
KTSearch.init();
});


When I try to run the code, it tells me it can't find select2:

Uncaught TypeError: $(...).select2 is not a function

But if I use data-control="select2" in html code, it works. why?


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (2)


Hi,

Sorry for the delay, and we miss looked into this issue. Could you please try to add the below code on top of this file?


require("select2");


Thanks



Try adding the select2 script to your application.


<link href=" rel="stylesheet" />
<script src="


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(