Ron Capili Ron Capili - 4 months ago 14
jQuery Question

How will transfer the value of textbox1 into textbox2 separated by comma per input

i tried this code. my problem is how will i transfer the value of text to holder it should be separated by comma per input and should not repeated the input value anyone can help me thank you in advance.

<input type="text" name="text" id="text">

<button type="button" id="button"> Button </button>
<Br> <Br>
<input type="holder" id="holder" name="holder">


<script>
$(document).ready(function() {

$("#button").click(function() {
$('#text').each(function() {
if ($(this).val() == $current.val() && $(this).attr('id') != $current.attr('id'))
{
alert('duplicate found!');
}
var data = [];
$.each($("#text"), function() {
data.push($(this).attr("value"));
});
$('#holder').val(data.join(","));;

});

});
</script>

Prs Prs
Answer

Try this out..

$("#button").click(function() {
  $text = $('#text');
  $textval = $text.val();
  $holder = $('#holder');
  $holderval = $holder.val();
  if ($holderval.trim().length) {
    var duplicate;
    duplicate = $holderval.split(',').filter(function(hv) {
      return hv == $textval;
    })
    if (duplicate.length) {
      alert("Duplicate found");
      return false;
    }
    $holder.val($holderval += ',' + $textval);
  } else {
    $holder.val($holderval += $textval);
  }
  $text.val('');
});

$("#button").click(function() {
  $text = $('#text');
  $textval = $text.val();
  $holder = $('#holder');
  $holderval = $holder.val();
  if ($holderval.trim().length) {
    var duplicate;
    duplicate = $holderval.split(',').filter(function(hv) {
      return hv == $textval;
    })
    if (duplicate.length) {
      alert("Duplicate found");
      return false;
    }
    $holder.val($holderval += ',' + $textval);
  } else {
    $holder.val($holderval += $textval);
  }
  $text.val('');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="text" id="text">

<button type="button" id="button">Button</button>
<Br>
<Br>
<input type="holder" id="holder" name="holder">