avinash ss
avinash ss

Reputation: 77

Input text value separated by commas

I tried:

success: function (result) {
    for (var i = 0; i < result.d.length; i++) {
        var emls = new Array();
        emls = result.d[i].Emailid;
        alert(emls);
        $("#EmailCC").val(emls);                                                                                        
    }
}

In the alert, I am getting all the values of the result but text box shows only the last value of the result.

Upvotes: 1

Views: 225

Answers (3)

Sabbir Ahmed
Sabbir Ahmed

Reputation: 11

Little modification here.

success: function (result) {
    var emls = [];
    for (var i = 0; i < result.d.length; i++) {
        emls.push(result.d[i].Emailid);                                                                                     
    }
    $("#EmailCC").val(emls.join(", "));
} 

Upvotes: 0

Pankaj Makwana
Pankaj Makwana

Reputation: 3050

You are overwriting email value each time. try below code

success: function (result) {
    var emails = "";
    for (var i = 0; i < result.d.length; i++) {
        var emls = new Array();
        emls = result.d[i].Emailid;
        emails += emls + ",";
    }
    $("#EmailCC").val(emails);

Upvotes: 1

Satpal
Satpal

Reputation: 133403

$("#EmailCC").val(emls); will overwrite the previous value, hence you are only getting the last email id.

Create an array of email ids using Array.map() then use Array.join() to create a comma separate string

success: function (result) {
    var emails = result.d.map(x => x.Emailid);
    $("#EmailCC").val(emails.join(','));
}

Upvotes: 2

Related Questions