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.



Try this-

$('select').on('change', function() {
  alert( this.value );
<script src=""></script>

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

You can also reference with onchange event-

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

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:

  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


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
answered 5 Months ago
$("#first").val(); // this will give you value of selected element. i.e. 1,2,3.
Tuesday, July 27, 2021
answered 5 Months ago

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

Wednesday, September 1, 2021
answered 4 Months ago


var options = $('#selectbox2').html();
//store original options
$("#selectbox1").change(function() {
    var selected = this.value.split(',');
    // get selected value and make it an array
    // 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;
            // check each value
            var values = this.value.split(',');
            for(i in values){
                if($.inArray(values[i], selected) > -1)
                    return false;            
            return true;  
    // 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
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 :