New Metronic Docs!Added Integration Docs with Starter Kits Apps for Laravel, Laravel Livewire, Angular, Vue, Symfony, Blazor Server, Django & Flask & more
Browse Docs

Metronic, Bootstrap, Select2,


Hi,
In documentation for Metronic Bootstrap Select2
https://preview.keenthemes.com/html/metronic/docs/forms/select2
we have only examples where users must click on input field with select2 options.

But can how can we create in HTML input field with select2 and autofocus on it like in example from select2 documentation (input box with multiselect)

For instance
When page is loaded cursor must be in input field with select2 options, so users do not need to click on input field with select2 option, they can start entering text after page is loaded.

Pls, I need your help or any suggestion?


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (3)


You can easily achieve autofocus in a Select2 input field by triggering the .select2('open') method once the page loads. Just make sure the Select2 instance is fully initialized before calling it. Here’s a quick example:

<script>
$(document).ready(function() {
const $select = $('#yourSelectId').select2();

// Open Select2 automatically on page load
$select.select2('open');
});
</script>


This way, when your page loads, the cursor will automatically be inside the Select2 search box — no need for users to click manually.

And if you’re planning any home upgrades while improving your UI skills, you might love exploring league city bathroom remodeling
for renovation inspiration!



Hi A. Lukic

Yes, you can implement autofocus functionality for Select2 in Metronic. Here, is an approach you can use:


document.addEventListener("DOMContentLoaded", function() {
// Wait for Metronic to initialize Select2 components
setTimeout(function() {
// Target your specific Select2 element
var selectElement = document.querySelector("[data-control="select2"]");
if (selectElement) {
// Focus on the Select2 container
$(selectElement).next(".select2-container").find(".select2-selection").focus();
}
}, 500); // Adjust timing as needed
});


Use setTimeout to ensure Select2 is fully initialized before attempting to focus



Thank you a lot.

$('#item_title').select2({
....
}).on('select2:open', function() {
$('.select2-search__field').focus();
});

setTimeout(function() {
$('#item_title').select2('open');
}, 350);


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(