javascript - Remove input value jquery -


each time click on option data-type should appear in input.

but want if value in .val of input should not appear anymore , if click twice want remove data-type input.

here jsfiddle:

$('.checkbox').on('click', function () {         var type = $(this).data('type'),                 answer = $('.answer'),                 initial = $('.answer').val();          $(this).toggleclass('checked');          if (answer.val().length === 0) {             answer.val(type);         } else {             answer.val(initial + ',' + type);         }       }); 

http://jsfiddle.net/xbwocrf3/

thanks!

one solution using jquery map:

$('.checkbox').on('click', function() {    $(this).toggleclass('checked');    //save values of checked in array    var answervalues = $(".checkbox.checked").map(function() {      return $(this).data("type");    }).get();      //update input text values    $(".answer").val(answervalues);  });
.checkbox.checked {    border: 2px solid green;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <input type="text" class="answer" />    <div class="checkbox" data-type="1">option #1</div>  <div class="checkbox" data-type="2">option #2</div>  <div class="checkbox" data-type="3">option #3</div>  <div class="checkbox" data-type="4">option #4</div>


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -