user3801032
user3801032

Reputation:

Dropdownlist to change text field. Bootstrap Jquery Ajax

I'm using a ajax call to populate my Select2 dropdownlist. I want the choice made from dropdown to populate some fields with data from my database. The text field should be editable and post new value to my database. (Edit-database-form with jquery) My problem is that i can´t get the text field populated.

HTML:

<input type="hidden" name="optionvalue" id="selectbox-o" class="col-sm-10" data-placeholder="Välj objekt..." />
<input type="text" class="form-control" id="selectedID" placeholder="Alias" />

JS:

$(document).ready(function(){
var test = $('#selectbox-o');
$('#selectbox-o').select2({

    ajax: {
    url: "include/objekt.php",
    dataType: 'json',
        data: function (term, page) {
            return {
            q: term
            };
        },
        results: function (data, page) {
        return { results: data };
        }
    } // Ajax Call
}); // Select2

// Start Change
$(test).change(function() {

    var theID = $(test).select2('data').id;
    var theSelection = $(test).select2('data').text;
    $('#selectedID').text(theID);
    $('#selectedText').text(theSelection);
}); //Change
}); //Domument Ready

Upvotes: 1

Views: 906

Answers (1)

user3801032
user3801032

Reputation:

The right JS code should be:

$('#selectedID').val(theId);

I changed .tex to .val and it worked! Thanks – Se0ng11

Upvotes: 1

Related Questions