How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Auto-focus a Select2 Dropdown on Page Load
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • 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.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Laravel Eloquent ORM — a model class mapping to a database table with query methods
Laravel Eloquent ORM: The Complete Guide to Querying Your Database
June 16, 2026
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026

You Might Also Like

CSS print styles shown in a clean print preview layout
Web Development

How to Add CSS Print Styles for Printer and Print Screen

7 Min Read
Install PHP on Ubuntu — terminal with apt install php command and stylized elephant icon
Web Development

How to Install PHP on Ubuntu (22.04 & 24.04): Step-by-Step Guide

9 Min Read
WordPress logged-in menu swap — register_nav_menus + wp_nav_menu with is_user_logged_in ternary
Web Development

How to Display Different Menus to Logged-In Users in WordPress

7 Min Read
Display PHP errors — ini_set + php.ini configuration
Web Development

How to Display PHP Errors

7 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • 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?