How to set selected value of jQuery Select2

0 votes

This belong to codes prior to Select2 version 4

I have a simple code of select2 that get data from AJAX.

$("#programid").select2({
  placeholder: "Select a Program",
  allowClear: true,
  minimumInputLength: 3,
  ajax: {
    url: "ajax.php",
    dataType: 'json',
    quietMillis: 200,
    data: function (term, page) {
      return {
        term: term, //search term
        flag: 'selectprogram',
        page: page // page number
      };
    },
    results: function (data) {
      return {results: data};
    }
  },
  dropdownCssClass: "bigdrop",
  escapeMarkup: function (m) { return m; }
});

This code is working, however, I need to set a value on it as if in edit mode. When user select a value first time, it will be saved and when he needs to edit that value it must appear in the same select menu (select2) to select the value previously selected but I can't find a way.

UPDATE:

The HTML code:

<input type="hidden" name="programid" id="programid" class="width-500 validate[required]">

Select2 programmatic access does not work with this.

May 27, 2022 in Java by Edureka
• 13,690 points
40,664 views

1 answer to this question.

0 votes

The third parameter of the new Option (...) determines if the item is "selected by default". That is, it sets the selected attributes of the new option. The fourth parameter sets the currently selected status of the option. If set to true, the new option will be selected by default.

Create if it does not exist 
You can use .find to select an option if it already exists, otherwise you can create it.

// Set the value, creating a new option if necessary
if ($('#mySelect2').find("option[value='" + data.id + "']").length) {
    $('#mySelect2').val(data.id).trigger('change');
} else { 
    // Create a DOM Option and pre-select by default
    var newOption = new Option(data.text, data.id, true, true);
    // Append it to the select
    $('#mySelect2').append(newOption).trigger('change');
} 
answered May 30, 2022 by gaurav
• 23,260 points

Related Questions In Java

0 votes
1 answer

How can I set JFrame to appear centered, regardless of monitor resolution?

Hello @kartik, Use setLocationRelativeTo(null) This method has a special effect ...READ MORE

answered Apr 21, 2020 in Java by Niroj
• 82,840 points
1,749 views
0 votes
1 answer

How to set AUTO-SCROLLING of JTextArea in Java GUI?

Hii kartik, When you click anywhere over JTextArea, ...READ MORE

answered Apr 21, 2020 in Java by Niroj
• 82,840 points
2,486 views
0 votes
0 answers

How to manage two JRadioButtons in java so that only one of them can be selected at a time?

How to manage two JRadioButtons in java ...READ MORE

Apr 21, 2020 in Java by kartik
• 37,520 points
723 views
0 votes
1 answer

How to set JFrame to appear centered, regardless of monitor resolution?

Hello kartik, You can  always did it in ...READ MORE

answered May 7, 2020 in Java by Niroj
• 82,840 points
635 views
0 votes
0 answers

How to convert an Object {} to an Array [] of key-value pairs in JavaScript

I'd want to transform the following object: {"1":5,"2":7,"3":0,"4" ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
1,149 views
0 votes
1 answer

how to get ascii value of char in java

In Java, you can get the ASCII ...READ MORE

answered Nov 24, 2023 in Java by Zoya
410 views
0 votes
2 answers

How to test that an array contains a certain value?

public static final String[] VALUES = new ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,920 points
1,115 views
0 votes
2 answers

How to round up a value to 2 decimal places?

double d = 2.34568; DecimalFormat f = new ...READ MORE

answered Aug 30, 2018 in Java by Sushmita
• 6,920 points
1,378 views
0 votes
1 answer

Using setTimeout to delay timing of jQuery actions

The JavaScript setTimeout () function  is used ...READ MORE

answered May 30, 2022 in Java by gaurav
• 23,260 points
8,051 views
0 votes
1 answer

Get selected text from a drop-down list (select box) using jQuery

Select elements typically have two values that ...READ MORE

answered May 30, 2022 in Java by gaurav
• 23,260 points
3,251 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP