mastersuse
mastersuse

Reputation: 988

How to remove space in jQuery using Select2 plugin

in database the value is saved as Jack, Mike which have space between , and M. But when I want to display at the frontend in edit form using dropdown select2 plugin, an error appear at console. It seems like I need to remove the space in between , M then it will appear in the dropdown select2. the question how to remove the space during to display at the dropdown edit form?

Currently it just display the first name only which is Jack.

DB: Jack, Mike

JS

$("#editLayer2TaskOwner").val(response.data[0]["task_owner"]);

// To allow Select2 to choose selected as stored from DB
$(response.data).each(function(key,value){
    var owners = value.task_owner.split(',');
    $(owners).each(function(k,v){
        $("#editLayer2TaskOwner").append($("<option selected>", {
            response: v,
            text: v
        }));
    });
    $("#editLayer2TaskOwner").val(owners).trigger("change");
});

Upvotes: 0

Views: 225

Answers (1)

Senthurkumaran
Senthurkumaran

Reputation: 1858

It very easy just split with , and space. It will work. change your code like below.

$("#editLayer2TaskOwner").val(response.data[0]["task_owner"]);
// To allow Select2 to choose selected as stored from DB
$(response.data).each(function(key,value){
    var owners = value.task_owner.split(', ');
    $(owners).each(function(k,v){
        $("#editLayer2TaskOwner").append($("<option selected>", {
            response: v,
            text: v
        }));
    });
    $("#editLayer2TaskOwner").val(owners).trigger("change");
});

Upvotes: 1

Related Questions