1

I am trying to input the value of a checkbox into a text input.

Let's say that the input box is empty - you click on the checkbox, and the value assigned to the checbkox is being shown inside the input box.

$('input.lowercase').on('change', function(){
      if ( $(this).is(':checked') ) {
$("input.qwer").on("keyup",function () {
    $("input.qwer").html($(this).val());
}); } } );

No matter what I do I can't get this to work. Any help? http://jsfiddle.net/6ycnzrty/

4 Answers 4

5

[EDITED] (As per your needs)

Demo on Fiddle

HTML:

<input type="text" class="output" value="" />
<br>
<input type="checkbox" class="qwer" value="qwerty">Input value of this checkbox(qwert)
<br>
<input type="checkbox" class="numbers" value="1234567890">Input value of this checkbox(numbers)
<br>
<input type="checkbox" class="asdfg" value="asdfg">Input value of this checkbox(asdfg)
<br>
<input type="checkbox" class="zxcvb" value="zxcvb">Input value of this checkbox(zxcvb)

JavaScript:

$('input.qwer').on('change', function () {
    if ($(this).is(':checked')) {
        $('.output').val($('.output').val() + $(this).val());
    } else {
        $('.output').val($('.output').val().replace($('.qwer').val(), ''));
    }
});

$('input.numbers').on('change', function () {
    if ($(this).is(':checked')) {
        $('.output').val($('.output').val() + $(this).val());
    } else {
        $('.output').val($('.output').val().replace($('.numbers').val(), ''));
    }
});

$('input.asdfg').on('change', function () {
    if ($(this).is(':checked')) {
        $('.output').val($('.output').val() + $(this).val());
    } else {
        $('.output').val($('.output').val().replace($('.asdfg').val(), ''));
    }
});

$('input.zxcvb').on('change', function () {
    if ($(this).is(':checked')) {
        $('.output').val($('.output').val() + $(this).val());
    } else {
        $('.output').val($('.output').val().replace($('.zxcvb').val(), ''));
    }
});
Sign up to request clarification or add additional context in comments.

13 Comments

Quick question, if I add second checkbox with other value, and check it, how can I make those two values exist in the text input box? Because with this solution one checkbox value is being replaced by other one. Appreciate your help!
Do you want to add two values to one textbox?
Yes. Basically multiple values from multiple checkboxes added to one textbox. This is embarassing because 2 days ago I had it working and somehow I am unable now to recreate it.
Okay, I've edited my answer and the link to the fiddle. Let me know if this is what you want.
It works nicely :-) Is there any way to remove the added value from checkbox on uncheck?
|
2

Try This :-

$('input.qwer').on('change', function(){
   if ( $(this).is(':checked') ) {
     $("input.output").val($(this).val());
   } 
   else{ $("input.output").val("123"); }
});

With above code if checkbox is unchecked then textbox having class 'output' will get its initial view i.e '123',if you don't need this functionality then try this :

$('input.qwer').on('change', function(){
   if ( $(this).is(':checked') ) {
     $("input.output").val($(this).val());
   } 
});

EDIT :-

DEMO

Comments

1

Try

var $chk = $('input.qwer').on('change', function () {
    //if the checkbox is checked and output is empty set the value
    if (this.checked && !$output.val()) {
        $output.val(this.value)
    }
});
var $output = $("input.output").on("change", function () {
    //when the value of output is changed as empty and checkbox is checked then set the value to checkbox
    if (!this.value && $chk.is(':checked')) {
        this.value = $chk.val();
    }
});

Demo: Fiddle

Comments

0
$("input.qwer").on("change",function () {
    if($(this).is(":checked"))
    $("input.output").val($(this).val());
    else
        $("input.output").val("123");
});

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.