How7oHow7o
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Auto-focus a Select2 Dropdown on Page Load
Share
Notification Show More
Font ResizerAa
How7oHow7o
Font ResizerAa
  • Tools
  • Prank Screens
  • Learn
  • Blog
Search
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Auto-focus a Select2 Dropdown on Page Load
Web Development

How to Auto-focus a Select2 Dropdown on Page Load

how7o
By how7o
Last updated: May 22, 2026
4 Min Read
Select2 auto-focus on page load — .select2('open')
SHARE

To auto-focus a Select2 dropdown on page load, call $('#mySelect2').select2('open') after the element has been initialized as a Select2 and after the DOM is ready. This both opens the dropdown and focuses the internal search input — Select2’s plain focus() doesn’t do this because Select2 hides the original <select> behind custom DOM.

Contents
  • TL;DR
  • Why .focus() doesn’t work
  • Order matters: init first, then open
  • Close programmatically too
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 with Select2 4.1.0+ and jQuery 3.7+. Originally published 2023-03-13, rewritten and updated 2026-05-17.

TL;DR

<select id="mySelect2">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3">Option 3</option>
</select>

<script>
$(function () {
  $('#mySelect2').select2();        // initialize
  $('#mySelect2').select2('open');  // open + focus the search box
});
</script>

Why .focus() doesn’t work

Select2 hides the original <select> element and replaces it with its own DOM — a styled container plus an internal search input that only appears when the dropdown is open. Calling $('#mySelect2').focus() focuses the hidden native element, not anything the user can see.

The Select2 API has a method for this exact use case: .select2('open'). It opens the dropdown and, in the same call, focuses the internal search input — equivalent to the user clicking the Select2 control with the mouse.

Select2 auto-focus on page load — select2('open') opens dropdown and focuses search

Order matters: init first, then open

$(function () {
  // 1. Initialize as a Select2 (does nothing yet)
  $('#mySelect2').select2({
    placeholder: 'Choose one',
    width: 'resolve',
  });

  // 2. Then ask Select2 to open it
  $('#mySelect2').select2('open');
});

Calling .select2('open') before .select2() throws “select2 is not a function” — the plugin only attaches its API methods after initialization.

Close programmatically too

// Open
$('#mySelect2').select2('open');

// Close
$('#mySelect2').select2('close');

// Pre-select an option and close
$('#mySelect2').val('2').trigger('change').select2('close');

Frequently asked questions

Why doesn’t $('#mySelect2').focus() open the dropdown?

Because Select2 replaces the native <select> with a hidden input wrapped in custom DOM. Calling focus() on the original element focuses something the user can’t see. Select2 exposes a programmatic API for this exact case — .select2('open') — that opens the dropdown and focuses the internal search input.

Where in my code should I call .select2('open')?

After the element is initialized as a Select2 and after the DOM is ready. The safest spot is inside a $(document).ready(...) handler, immediately after the .select2() initialization call: $('#mySelect2').select2(); $('#mySelect2').select2('open');.

Does this work with multiple Select2s on the page?

Yes, but only one can be open at a time — opening a second auto-closes the first. If you really need them all ‘pre-open’ you can’t (Select2 by design), but you can open the first and let the user tab to the others.

Why open it on page load at all?

Two common reasons: (1) you have a single-purpose page (an order entry form, a search interface) where the very first action is to pick from the dropdown — opening it saves a click; (2) for accessibility, focus + open signals ‘start here’ to screen readers without a separate label.

Can I close it programmatically too?

$('#mySelect2').select2('close'). Mirrors the open method. Useful for closing after a programmatic selection: $('#mySelect2').val('2').trigger('change').select2('close');.

Related guides

  • How to Add the Required Attribute to Input Fields with jQuery
  • How to Check if a JavaScript String Is a Valid URL

References

Select2 programmatic control reference: select2.org/programmatic-control/methods.

TAGGED:JavaScriptjQueryselect2

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article PHP Bangladeshi number format — 1,00,23,456.79 grouping Format Numbers in Bangladeshi / Indian Style with PHP
Next Article XAMPP autostart Apache and MySQL on Windows — XAMPP Control Panel and services How to Auto-start Apache and MySQL with XAMPP on Windows
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

You Might Also Like

MySQL extract digits from string — REGEXP_REPLACE negation class
Web Development

How to Extract Only the Digits from a String in MySQL

6 Min Read
Make Select2 work inside a Bootstrap modal
Web Development

How to Use Select2 Inside a Bootstrap Modal

4 Min Read
Rename menu items on the WooCommerce My Account page
Web Development

How to Rename Menu Items on the WooCommerce My Account Page

4 Min Read
WooCommerce auto add to cart on visit — template_redirect hook and cart dedup
Web Development

How to Automatically Add a Product to Cart on Visit in WooCommerce

8 Min Read
How7oHow7o

Free browser-based tools, prank screens, and step-by-step tech tutorials. No signup, nothing uploaded.

  • Private by design — your files never leave your device
  • Instant — no signup, no install, no waiting
  • Always free — no trials, no watermarks, no paywalls

Tools

  • PDF Editor
  • Image Upscaler
  • JPEG Compressor
  • Password Generator
  • QR Code Generator
  • Video Trimmer
  • See all tools →

Pranks

  • Cracked Screen Prank
  • Fake Blue Screen (BSOD)
  • Fake Disk Format
  • Fake Low Battery
  • Fake Nintendo Switch Update Screen
  • Fake PlayStation Update Screen
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?