How To Set Selected Value Of JQuery Select2 | Edureka Community
Có thể bạn quan tâm
- Home
- Community
- Categories
- Java
- How to get select2 selected value in Jquery
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 |
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'); }- flag
- ask related question
- 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 answerHow 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
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
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
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
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
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
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
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
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
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 (1,454) -
Power BI (1,316) -
DevOps & Agile (4,138) -
Data Science (100) -
ChatGPT (30) -
Cyber Security & Ethical Hacking (1,057) -
Data Analytics (1,266) -
Cloud Computing (4,053) -
Machine Learning (337) -
PMP (1,069) -
Python (3,488) -
SalesForce (201) -
Selenium (1,624) -
Software Testing (58) -
Tableau (608) -
Web Development (3,972) -
UI UX Design (24) -
Java (1,358) -
Azure (157) -
Database (858) -
Big Data Hadoop (1,907) -
Blockchain (1,673) -
Digital Marketing (121) -
C# (141) -
C++ (272) -
IoT (Internet of Things) (390) -
Kotlin (8) -
Linux Administration (389) -
MicroStrategy (7) -
Mobile Development (395) -
Others (2,386) -
RPA (653) -
Talend (73) -
TypeSript (124) -
Apache Kafka (84) -
Apache Spark (596) -
Career Counselling (1,091) -
Events & Trending Topics (28) -
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 FacebookOR
Password must haveAt 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 haveAt 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 UpSubscribe to our Newsletter, and get personalized recommendations.
Already have an account? Sign in.
Từ khóa » Html Select Set Selected Value Jquery
-
Set Select Option 'selected', By Value - Jquery - Stack Overflow
-
How To Set Selected Value On Dropdownlist Using Jquery - CodeProject
-
How To Change Selected Value Of A Drop-down List Using JQuery?
-
Jquery Set Select Selected Value Code Example - Code Grepper
-
JQuery Set Selected Value Of Dropdown - Tuts Make
-
HTML DOM Select Value Property - W3Schools
-
How Does JQuery Select Value Work? - EduCBA
-
How Do I Get The Text Value Of A Selected Option?
-
Change The Selected Value Of A Drop-down List Using JQuery
-
How To Get The Value Of Selected Option In A Select Box Using JQuery
-
Set Selected Value Not Working - Material Design For Bootstrap
-
Change Selected Option In JavaScript [With Examples] - Alvaro Trigo
-
Jquery Set Select Option Value - Pakainfo
-
Get Selected Value In Drop-down In JQuery - Javatpoint
Generative AI
Power BI
DevOps & Agile
Cyber Security & Ethical Hacking
Data Analytics
Cloud Computing
Machine Learning
Python
Selenium
Software Testing
Tableau
Web Development
Java
Database
Big Data Hadoop
Blockchain
IoT (Internet of Things)
Linux Administration
MicroStrategy
RPA
Talend
Apache Kafka
Apache Spark
Career Counselling
Events & Trending Topics