Create and Delete input type text if the selected input type checkbox

Cristian Meza

I have a problem, I need to create or delete as appropriate, a Input type text if a Input type checkbox is selected.

At the moment I can only create the input text, but I manage to find the solution to clear if the checkbox is unchecked.

My HTML code:

<form role="form" action="" method="POST">
  <input type="checkbox" class="myCheckBox" name="category[]" value="1">
  <input type="checkbox" class="myCheckBox" name="category[]" value="2">
  <input type="checkbox" class="myCheckBox" name="category[]" value="3">
  <input type="checkbox" class="myCheckBox" name="category[]" value="4">
  <input type="checkbox" class="myCheckBox" name="category[]" value="5">
  <div class="inputCreate"></div>
  <input type="submit" name="" value="Send yours categories">
</form>

And the jQuery code is as follows

     var wrapper = $(".inputCreate");

$('.myCheckBox').click(function() {
    if ($(this).is(':checked')) {
         $(wrapper).append('<input type="text" name="mytext[]" placeholder="">');
    }
});

As I can do that by unchecking the checkbox, also delete the input text field ?.

Solution to this problem:

var wrapper = $(".inputCreate");
$(".myCheckBox").change( function(){
    if($(this).is(':checked')){
         $(wrapper).append('<input type="text" name="mytext[]" placeholder="">');
    }
    else{
        $('.inputCreate :last-child').remove();
    }
});

Greeting From Chile.

Prashant Ghimire

This should be exactly what you want: Working Fiddle

var wrapper = $(".inputCreate");
$(".myCheckBox").change( function(){
    if($(this).is(':checked')){
         $(wrapper).append('<input type="text" name="mytext[]" placeholder="'+$(this).val()+'">');
    }
    else{
        $('.inputCreate :last-child').remove();
    }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Associate input type checkbox to input type text

From Dev

How to delete an input type text?

From Dev

$(':checkbox') or $('input[type="checkbox"]')

From Dev

How to Create Different Styles for input[type=checkbox]?

From Dev

Create element from input type='text' JavaScript

From Dev

Create Bootstrap dropdown from input[type="text"]

From Dev

Input type checkbox with MVC razor

From Dev

Changing "+" in textarea to <input type='checkbox'>

From Dev

input type checkbox cannot check

From Dev

Check checkbox if i write something in a input type text

From Dev

will input default to a type of text?

From Dev

Input type text with array

From Dev

Input type="text" to innerHTML

From Dev

input type checkbox vs asp:checkbox

From Dev

input type checkbox vs asp:checkbox

From Dev

<input type="checkbox"> vs <div role="checkbox">

From Dev

input connection-how to delete selected text?

From Dev

Create input type="button" with two lines of text for newer version of chrome

From Java

Multiple lines of input in <input type="text" />

From Dev

How to check input in Javascript for input type=text

From Dev

CSS checkbox display error input[type=checkbox]:checkbox

From Dev

Track changes in a input[type=text]

From Dev

Truncate text in input type = file

From Dev

preventDefault() not working in a input type text

From Dev

Calculation on a javascript input type=text

From Dev

React: input type="checkbox" onChange not triggered

From Dev

Fill a table cell with input type="checkbox"

From Dev

JQuery: select <p> inside an input checkbox type

From Dev

add checkbox on input type number change