• We just launched and are currently in beta. Join us as we build and grow the community.

Input Select using Select2 Plugin

SchlongLong85

Banana Enthusiast
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
58
Likes
21
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Adding Dependencies

In order to add select2, we need to include the plugin to our page. Note that files use in this tutorial is included in the downloadable.

Include the CSS and JS of select2 in the header section of your page of the JS file before the closing body tag.

  1. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "assets/select2.min.css"

    >
  2. <script

    src

    =

    "assets/jquery.min.js"

    ></

    script

    >
  3. <script

    src

    =

    "assets/select2.full.min.js"

    ></

    script

    >

Don't forget to include jQuery as well.

Initializing Select2

Next step is to initialize our select2 and assign it to a class.

  1. $(

    function

    (

    )

    {
  2. //Initialize Select2 Elements
  3. $(

    '.select2'

    )

    .select2

    (

    )
  4. }

    )

    ;

Creating the Select Input

Lastly, we create the select tag where we want to apply select2.

  1. <select

    class

    =

    "form-control select2"

    name

    =

    "fruit"

    >
  2. <option

    value

    =

    ""

    selected>

    - Select Fruit -</

    option

    >
  3. <option

    >

    Apple</

    option

    >
  4. <option

    >

    Orange</

    option

    >
  5. <option

    >

    Strawberry</

    option

    >
  6. <option

    >

    Mango</

    option

    >
  7. <option

    >

    Watermelon</

    option

    >
  8. <option

    >

    Melon</

    option

    >
  9. <option

    >

    Jackfruit</

    option

    >
  10. <option

    >

    Star Apple</

    option

    >
  11. <option

    >

    Buko</

    option

    >
  12. <option

    >

    Guava</

    option

    >
  13. <option

    >

    Peach</

    option

    >
  14. <option

    >

    Pears</

    option

    >
  15. <option

    >

    Chico</

    option

    >
  16. <option

    >

    Durian</

    option

    >
  17. </

    select

    >

Here's the full html.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Input Select using Select2 Plugin</title>
  6. <link rel="stylesheet" type="text/css" href="bootstrap4/css/bootstrap.min.css">
  7. <link rel="stylesheet" type="text/css" href="assets/select2.min.css">
  8. </head>
  9. <body>
  10. <div class="container">
  11. <h1 class="text-center" style="margin-top:30px;">Input Select using Select2 Plugin</h1>
  12. <hr>
  13. <div class="row justify-content-center">
  14. <div class="col-sm-4">
  15. <?php
  16. if

    (

    isset

    (

    $_POST

    [

    'submit'

    ]

    )

    )

    {
  17. $fruit

    =

    $_POST

    [

    'fruit'

    ]

    ;
  18. echo

    "<p>You selected: <b>"

    .

    $fruit

    .

    "</b></p>"

    ;
  19. }
  20. ?>
  21. <form method="POST">
  22. <div class="form-group">
  23. <select class="form-control select2" name="fruit">
  24. <option value="" selected>- Select Fruit -</option>
  25. <option>Apple</option>
  26. <option>Orange</option>
  27. <option>Strawberry</option>
  28. <option>Mango</option>
  29. <option>Watermelon</option>
  30. <option>Melon</option>
  31. <option>Jackfruit</option>
  32. <option>Star Apple</option>
  33. <option>Buko</option>
  34. <option>Guava</option>
  35. <option>Peach</option>
  36. <option>Pears</option>
  37. <option>Chico</option>
  38. <option>Durian</option>
  39. </select>
  40. </div>
  41. <button type="submit" class="btn btn-primary" name="submit">Submit</button>
  42. </form>
  43. </div>
  44. </div>
  45. </div>

  46. <script src="assets/jquery.min.js"></script>
  47. <script src="assets/select2.full.min.js"></script>
  48. <script>
  49. $(function(){
  50. //Initialize Select2 Elements
  51. $('.select2').select2()
  52. });
  53. </script>
  54. </body>
  55. </html>

That ends this tutorial. Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top