1

I have a table with 4 checkboxes. Lets say with Title,Adress,Postal Code and Email.

<table class="mytable">
         <tbody>
         <tr>
         <td> <input type="checkbox" value="title"> title </td>
        <td> <input type="checkbox" value="adress">  Adress </td>
        <td> <input type="checkbox" value="postalcode">  Postal Code </td>
        <td> <input type="checkbox" value="email">  Email </td>
         </tr>
</table>

I have another div with class "myfilters" which is hidden when none of the above checkboxes are checked.

<div class="myfilters">
 <button type="button" class="btn btn-default btn-sm"> Title </button>

 <button type="button" class="btn btn-default btn-sm"> Adress</button>       
</div>

My question is How can I implement when a user checks lets say "title" and "adress" checkboxes in the table then inside the div "myfilters" to be created dynamically the buttons "Title" and the Button "Address" ?

2

4 Answers 4

3

Don't add the buttons dynamically. Create them in the HTML, but hide them with CSS. Then show them when you click on the corresponding checkbox.

$(":checkbox").click(function() {
  $("#"+this.value).toggle(this.checked);
});
.btn {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="mytable">
         <tbody>
         <tr>
         <td> <input type="checkbox" value="title"> title </td>
        <td> <input type="checkbox" value="adress">  Adress </td>
        <td> <input type="checkbox" value="postalcode">  Postal Code </td>
        <td> <input type="checkbox" value="email">  Email </td>
         </tr>
</table>

<div class="myfilters">
 <button type="button" id="title" class="btn btn-default btn-sm"> Title </button>

 <button type="button" id="adress" class="btn btn-default btn-sm"> Adress</button>   
  <button type="button" id="postalcode" class="btn btn-default btn-sm"> Postal Code</button>  
  <button type="button" id="email" class="btn btn-default btn-sm"> Email</button>  
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

+1, This solution will run faster than js scripts that are creating and appending html to the DOM.
1

try this :

$(function(){
  $('.mytable').on("change","input[type=checkbox]",function(){
      if($(this).is(':checked'))
      {
        var button = '<button type="button" class="btn btn-default btn-sm" id="'+$(this).val()+'">'+$(this).parent().text()+'</button>';
        $('.myfilters').append(button);
      }
      else
      {
         $('.myfilters').find('button[id="'+$(this).val()+'"]').remove();
      }
  });
});

Demo

Comments

0

This is what you need (notice that I've added the IDs for checkboxes):

<table class="mytable">
  <tr>
    <td><input id="c1" type="checkbox" value="title">Title</td>
    <td><input id="c2" type="checkbox" value="adress">Address</td>
    <td><input id="c3" type="checkbox" value="postalcode">Postal Code</td>
    <td><input id="c4" type="checkbox" value="email">Email</td>
  </tr>
</table>

<script>
$(document).ready(function(){
  $("#c1").click(function(){
    $(".myfilers").append("<button>Title</button>");
  });

  $("#c2").click(function(){
    $(".myfilers").append("<button>Address</button>");
  });

  $("#c3").click(function(){
    $(".myfilers").append("<button>Postal Code</button>");
  });

  $("#c4").click(function(){
    $(".myfilers").append("<button>Email</button>");
  });
});
</script>

Comments

0

Here is a fiddle Demo, FIDDLE DEMO CLICK HERE

 var myNull=null; // ignore this line

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.