How to get select2 selected value in Jquery

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
41,534 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 to get an enum value from a string value in Java?

Hello @kartik, Yes, Blah.valueOf("A") will give you Blah.A. Note that the name ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,840 points
2,183 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
473 views
0 votes
1 answer

How to get the number of digits in an int?

You can find out the length of ...READ MORE

answered May 14, 2018 in Java by Akrati
• 3,190 points
1,046 views
0 votes
1 answer

How to get a platform-dependent new line character in Java?

If you are using Java 1.5 or ...READ MORE

answered Jul 3, 2018 in Java by Akrati
• 3,190 points
968 views
0 votes
1 answer

How to get rid of TLE in Java?

The TLE (Time limit exceed) problem occurs ...READ MORE

answered Mar 5, 2019 in Java by Disha
2,716 views
0 votes
1 answer

How to get text from text box in Swing?

You can add a Button listener here. ...READ MORE

answered Aug 27, 2019 in Java by Jishan
1,416 views
0 votes
1 answer

how to get latest modified object or folder in s3 bucket in java.

Here's what you could probably try: List all ...READ MORE

answered Apr 6, 2020 in Java by Liana
8,716 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
831 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,392 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,238 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