How To Set Selected Value Of JQuery Select2 | Edureka Community

179090/how-to-set-selected-value-of-jquery-select2

  • Home
  • Community
  • Categories
  • Java
  • How to get select2 selected value in Jquery
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.

  • javascriptphpjqueryajaxjquery-select2
May 27, 2022 in Java by Edureka 13,690 points 43,697 views answer comment
  • flag

Your comment on this question:

Your name to display (optional):
Email me at this address if a comment is added after mine:
Privacy: Your email address will only be used for sending these notifications.
Add comment Cancel

1 answer to this question.

Your answer

Your name to display (optional):
Email me at this address if my answer is selected or commented on:
Privacy: Your email address will only be used for sending these notifications.
Add answer Cancel
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,580 points comment
  • flag
  • ask related question
Worked & Thanks commented Feb 2, 2024 by Jagdeep Singh edited Mar 6
  • flag
  • reply

Your comment on this answer:

Your name to display (optional):
Email me at this address if a comment is added after mine:
Privacy: Your email address will only be used for sending these notifications.
Add comment Cancel

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,800 points 2,829 views
  • java
  • selenium-java
  • selenium-webdriver
  • selenium
  • java-programming
  • java-strings
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 1,077 views
  • java
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,884 views
  • java
  • data-types
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 1,538 views
  • java
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 3,268 views
  • java
  • java-programming
  • java-array
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,906 views
  • java
  • java-swing
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 9,502 views
  • amazon-web-services
  • s3
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 1,264 views
  • java
  • c
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,580 points 3,991 views
  • javascriptjquerydomdrop-down-menujquery-selectors
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,580 points 8,996 views
  • javascriptjquery

Recent in Java

  • how do I turn a double in an array from 1000 to infinity Jul 9, 2024
  • I have created a code in java for a flower shop and now my IDE is showing all the inputs are in red showing there is an error Jul 6, 2024
  • This question for testing purpose May 9, 2024
  • dwdewwwwdsad May 6, 2024
  • How to display two independent different drop down list (different list) in Excel using Apache POI API Apr 4, 2024
  • All categories
  • Generative AI Generative AI (1,454)
  • Power BI Power BI (1,316)
  • DevOps & Agile DevOps & Agile (4,138)
  • Data Science Data Science (100)
  • ChatGPT ChatGPT (30)
  • Cyber Security & Ethical Hacking Cyber Security & Ethical Hacking (1,057)
  • Data Analytics Data Analytics (1,266)
  • Cloud Computing Cloud Computing (4,053)
  • Machine Learning Machine Learning (337)
  • PMP PMP (1,069)
  • Python Python (3,488)
  • SalesForce SalesForce (201)
  • Selenium Selenium (1,624)
  • Software Testing Software Testing (58)
  • Tableau Tableau (608)
  • Web Development Web Development (3,972)
  • UI UX Design UI UX Design (24)
  • Java Java (1,358)
  • Azure Azure (157)
  • Database Database (858)
  • Big Data Hadoop Big Data Hadoop (1,907)
  • Blockchain Blockchain (1,673)
  • Digital Marketing Digital Marketing (121)
  • C# C# (141)
  • C++ C++ (272)
  • IoT (Internet of Things) IoT (Internet of Things) (390)
  • Kotlin Kotlin (8)
  • Linux Administration Linux Administration (389)
  • MicroStrategy MicroStrategy (7)
  • Mobile Development Mobile Development (395)
  • Others Others (2,386)
  • RPA RPA (653)
  • Talend Talend (73)
  • TypeSript TypeSript (124)
  • Apache Kafka Apache Kafka (84)
  • Apache Spark Apache Spark (596)
  • Career Counselling Career Counselling (1,091)
  • Events & Trending Topics Events & Trending Topics (28)
  • Ask us Anything! Ask us Anything! (71)

Join the world's most active Tech Community!

Welcome back to the World's most active Tech Community!

Sign up with Gmail Sign up with Facebook

OR

Password must have

At least 1 upper-case and 1 lower-case letter

Minimum 8 characters and Maximum 50 characters

SIGN UP Already have an Edureka Account? Login Forgot Password? LOGIN Don’t have edureka account? Sign Up resend ? Password must have

At least 1 upper-case and 1 lower-case letter

Minimum 8 characters and Maximum 50 characters

reset password Don’t have edureka account? Sign Up reset password Don’t have edureka account? Sign Up Send Code Don’t have edureka account? Sign Up

Subscribe to our Newsletter, and get personalized recommendations.

Google Sign up with Google facebook Signup with Facebook

Already have an account? Sign in.

webinar REGISTER FOR FREE WEBINAR X Years of Experience* Student 0-2 Years 2-5 Years 5-10 Years 10+ Years REGISTER NOW webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP "PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.

Từ khóa » Html Select Set Selected Value Jquery