Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I need to get select2 dropdown selected value on button click event.

actually what am doing is when i entered mobile number in textbox and click search button all data will show in textbox and dropdown. textbox values came but this select2 dropdown is not selected.

I tried all the methods but not working. please suggest me..

What I have tried:

function edit(){      
        var mobile_no = document.getElementById('ddlMobileSrch').value;
<pre> $.ajax({
                      type : "POST",
                      url  : "<?php echo site_url('index.php/customer/edit_content')?>",
                      dataType : "JSON",
                      data : {mobile_no:mobile_no},
                      success: function(data){ 
                       $.each(data.custdetails, function(i, val) {   
$("#ddlBillArea").val(val.billing_area_code).trigger('change');
 $('[name="ddlBillArea"]').val(val.billing_area_code).trigger('change');

$("#ddlBillArea").select2("trigger", "select", {
data: { id: val.billing_area_code } 
});
 });   
Posted
Comments
F-ES Sitecore 8-Oct-19 7:13am    
$("#ddlBillArea").select2

that code is calling the select2 jQuery function on your ddlBillArea object. If you want to target a dropdown with an id\name of select2 then you need to use the right query selector, you can't use it as a function name and expect jquery to know what you mean.
Richard Deeming 8-Oct-19 7:37am    
Does the item you're trying to select already exist in the list?

Add, select, or clear items | Select2 - The jQuery replacement for select boxes[^]
Vivek.anand34 9-Oct-19 2:51am    
yes, thank you actually i removed text that'y why it didn't show text. i added text it works fine.
$("#ddlBillArea").select2("trigger", "select", {
data: { id: val.billing_area_code, text: val.bill_area_name }
});

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900