Try the below code. It works for ASP.NET; from your comment, I saw you used that.
var txt1 = document.getElementById('<%= txt1.ClientID %>').value;
var txt2 = document.getElementById('<%= txt2.ClientID %>').value;
var txt1values = txt1.split(",");
var txt2values = txt2.split(",");
var commonValues = [];
for (var i = 0; i < txt1values.length; i++) {
var curr = txt1values[i];
if (txt2values.indexOf(curr) != -1) {
commonValues.push(curr);
}
}
var commonValuesStr = commonValues.join(",");
document.getElementById('<%= txt3.ClientID %>').value = commonValuesStr;
How does this work? The ASP.NET server replaces
<%= txt1.ClientID %>
by the ID that can be used on the client side. Then, the code splits the text by a comma. Then, it loops over all values of the first textbox. Is that value also in the values of the second textbox? Then it gets added to
commonValues
. At the end, all values in
commonValues
get joined with a comma.