• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
seegatesite header

Seegatesite.com

Seegatesite.com - Programming Tutorial , Sharing , How and Learn Together

  • TOOLS
    • Bootstrap Navbar Online Generator
    • Customize Sidebar Menu Bootstrap 3
    • Bootstrap Demo
  • ADVERTISE
  • CATEGORIES
    • Android
    • Blogging Tips
    • Database
    • CSS
    • Info Gadget
    • Javascript
    • Linux
    • PHP
    • Various
    • WordPress
  • Q&A
  • PHP
  • JAVASCRIPT
  • JQUERY
  • ANGULAR
  • WORDPRESS
  • SEO
  • REACT
🏠 » Javascript » How To Load JSON Data From Rest API To Select2 JQuery Plugin

How To Load JSON Data From Rest API To Select2 JQuery Plugin

By Sigit Prasetya Nugroho ∙ April 22, 2019 ∙ Javascript ∙ Leave a Comment

Share : TwitterFacebookTelegramWhatsapp

A simple tutorial contains JSON data from the rest API on the select2 plugin using jquery. Hi friends, it’s been a long time since I made a new article in the midst of my busy life as a programmer. In this article, I will share a few tricks on how to display JSON data in our favorite select plugin, select2. Check out the following tutorial.

Maybe a lot of select2 tutorials have been shared in various sites. There are still many who ask me how to add data or load JSON data to select2. The method is quite easy, please follow the steps below

Tutorial How To Load JSON Data To Select2 Plugin

In my example, I use css bootstrap framework.

Create select / combobox elements in html

Related Articles :

  • Adept Using Datatables Plugin In 10 Minutes For Beginners (Tutorial)
  • How To Use JQuery Input File / Upload File Change Event
  • An Easy Way To Remove Attribute In HTML Element Using JQuery

1
2
3
4
5
6
7
8
<div class="container">
<h1>Load JSON Data To Select2 JQuery Plugin</h1>
<div class="row">
<div class="col">
<select class="select2"></select>
</div>
</div>
</div>

Add the following CSS so that the width of the select2 element can be maximum / 100%

1
2
3
4
5
6
.select2-container{
width: 100%!important;
}
.select2-search--dropdown .select2-search__field {
width: 98%;
}

The most important code is the following: JQuery code to load JSON data on select2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$(document).ready(function(){
$.ajax({
url: "https://jsonplaceholder.typicode.com/users",
type: "GET",
success: function(data, textStatus, jqXHR)
{
var xdata = $.map(data, function (obj) {
obj.text = obj.text || obj.name;  
return obj;
});
$(".select2").select2({
placeholder: "test",
data:xdata,
});
 
},
error: function (request, textStatus, errorThrown) {
swal("Error ", request.responseJSON.message, "error");
if(request.status == "401"){
alert("Unauthorized Access ");
return false;
}
}
});
});

The above method is quite short, and you can practice the application that you are developing.

I have another select2 tutorial “How To Use Select2 Remote AJAX With Example in JQuery, PHP And MySQL” maybe you can review it as learning material.

Demo : LOAD DATA JSON SELECT2 JQUERY PLUGIN

So, my short tutorial How to load JSON data to select2 with JQuery, I hope is useful.

Another Javascript Related Post :

  • React-Table Not Updating or Refreshing Data, The Solution ?
  • How To Custom React Datepicker In Bootstrap
  • Tutorial Create Simple POS Using ReactJS And Laravel Lumen Part 1
  • Adept Using Datatables Plugin In 10 Minutes For Beginners (Tutorial)
  • The Using Of Reactstrap And React-Table, Suitable For Beginners
  • Tutorial Create Simple Block UI Using React JS

Avatar for Sigit Prasetya Nugroho

About Sigit Prasetya Nugroho

This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Welcome to my Home,

Avatar for Sigit Prasetya NugrohoThis site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.



Popular Articles

Checked checkbox AdminLTE Bootstrap in Jquery

November 4, 2014 By Sigit Prasetya Nugroho 7 Comments

Simple create date format validation with jqueryUI

December 21, 2014 By Sigit Prasetya Nugroho Leave a Comment

Create Simple Progress Bar for Fake Online Generator with Jquery

January 10, 2015 By Sigit Prasetya Nugroho Leave a Comment

22+ Coolest Free Jquery Plugin For Premium Theme

October 3, 2015 By Sigit Prasetya Nugroho Leave a Comment

Easy Build Your Anti Copy Paste Plugin

October 6, 2015 By Sigit Prasetya Nugroho Leave a Comment

Popular Tags

adminlte (15) adsense (13) adsense tips (4) affiliate amazon (13) amazon (12) Android (8) angular (16) angular 4 (12) angular 5 (4) asin grabber (3) Bootstrap (27) codeigniter (5) create wordpress theme (5) crud (8) css (6) free wordpress theme (7) google adsense (4) imacros (4) increase traffic (6) jquery (34) laravel (10) laravel 5 (5) learn android (5) lumen api (4) modal dialog (5) mysql (6) nodeJs (4) optimize seo (4) pdo (6) php (30) plugin (53) pos (8) Publisher Tips (5) react (6) Reactjs (9) SEO (37) theme (17) tutorial angular (5) tutorial angular 4 (6) tutorial javascript (10) tutorial javascript beginners (4) twitter (3) wordpress (18) wordpress plugin (13) XMLRPC (5)




  • About
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

©2022 Seegatesite.com