Asked  7 Months ago    Answers:  5   Viewed   41 times

I was under the impression that I could get the value of a select input by doing this $(this).val(); and applying the onchange parameter to the select field.

It would appear it only works if I reference the ID.

How do I do it using this.

 Answers

35

Try this-

$('select').on('change', function() {
  alert( this.value );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select>
    <option value="1">One</option>
    <option value="2">Two</option>
</select>

You can also reference with onchange event-

function getval(sel)
{
    alert(sel.value);
}
<select onchange="getval(this);">
    <option value="1">One</option>
    <option value="2">Two</option>
</select>
Tuesday, June 1, 2021
 
BrunoRamalho
answered 7 Months ago
36

After looking at another answer it would seem I need to also pass id field with every call, otherwise it will disable the input.

Sample code that fixed it:

$('.select2').select2({
  placeholder: "Policy Name",
  minimumInputLength: 3,
  multiple: false,
  quietMillis: 100,
  id: function(bond){ return bond._id; },
  ajax: {
    url: "http://localhost:3000/search",
    dataType: 'json',
    type: 'POST',
    data: function(term, page) {
      return {
        search: term,
        page: page || 1
      }
    },
    results: function(bond, page) {
      return {results: bond.results, more: (bond.results && bond.results.length == 10 ? true: false)}
    }
  },
  formatResult: formatResult,
  formatSelection: formatSelection,
  initSelection: initSelection
})

Edit

Since this keeps getting upvoted I'll elaborate a bit. The .select2() method expects a unique id field on all results. Thankfully, there's a workaround. The id option accepts a function like this:

function( <INDIVIDUAL_RESULT> ) {
  // Expects you to return a unique identifier.
  // Ideally this should be from the results of the $.ajax() call. 
}

Since my unique identifier was <RESULT>._id, I simply return <RESULT>._id;

Sunday, July 11, 2021
 
Deyson
answered 5 Months ago
97
$("#first").val(); // this will give you value of selected element. i.e. 1,2,3.
Tuesday, July 27, 2021
 
weegee
answered 5 Months ago
87

Use the change event, which fires when the value of the element changes, like so:

$('#BezirksAuswahl').change(function(e){
    alert(this.value);
});
Wednesday, September 1, 2021
 
HaNdTriX
answered 4 Months ago
91

Proof: http://jsfiddle.net/iambriansreed/KBKEV/

var options = $('#selectbox2').html();
//store original options
$("#selectbox1").change(function() {
    var selected = this.value.split(',');
    // get selected value and make it an array
    $('#selectbox2').html(options);
    // reset the box to the original options
    $('#selectbox2 option').filter(function(){
        if(this.value.indexOf(',') == -1){
            // simple single values
            return $.inArray(this.value, selected) == -1;
        }else{  
            // check each value
            var values = this.value.split(',');
            for(i in values){
                if($.inArray(values[i], selected) > -1)
                    return false;            
            }
            return true;  
        }  
    }).remove();    
    // remove any options not in the array of selected values
});‚Äč

A working fiddle and a step by step explanation. Another quality answer.

Monday, November 22, 2021
 
ngourley
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share