Uncheck checkbox on click button
I am appending button on checkbox click, but need to uncheck when I close the appended button.
$(".left-menu input[name='freeAsset']").click(function()
if ($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">' + $(this).parent('div').children('span').text() + '<span aria-hidden="true" id="' + this.id + '">×</span></button></li>');
$('.filter-label li .btn-close span').click(function()
$(this).closest('li').remove();
);
else
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() + ")").filter(function()
return $(this).parent('li').length > 0;
).remove();
);
FIDDLE DEMO
jquery
add a comment |
I am appending button on checkbox click, but need to uncheck when I close the appended button.
$(".left-menu input[name='freeAsset']").click(function()
if ($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">' + $(this).parent('div').children('span').text() + '<span aria-hidden="true" id="' + this.id + '">×</span></button></li>');
$('.filter-label li .btn-close span').click(function()
$(this).closest('li').remove();
);
else
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() + ")").filter(function()
return $(this).parent('li').length > 0;
).remove();
);
FIDDLE DEMO
jquery
do you want to uncheck the checkbox after closing the button?
– Kiranramchandran
Nov 15 '18 at 12:21
add a comment |
I am appending button on checkbox click, but need to uncheck when I close the appended button.
$(".left-menu input[name='freeAsset']").click(function()
if ($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">' + $(this).parent('div').children('span').text() + '<span aria-hidden="true" id="' + this.id + '">×</span></button></li>');
$('.filter-label li .btn-close span').click(function()
$(this).closest('li').remove();
);
else
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() + ")").filter(function()
return $(this).parent('li').length > 0;
).remove();
);
FIDDLE DEMO
jquery
I am appending button on checkbox click, but need to uncheck when I close the appended button.
$(".left-menu input[name='freeAsset']").click(function()
if ($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">' + $(this).parent('div').children('span').text() + '<span aria-hidden="true" id="' + this.id + '">×</span></button></li>');
$('.filter-label li .btn-close span').click(function()
$(this).closest('li').remove();
);
else
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() + ")").filter(function()
return $(this).parent('li').length > 0;
).remove();
);
FIDDLE DEMO
jquery
jquery
edited Nov 15 '18 at 12:06
Zakaria Acharki
55.8k134471
55.8k134471
asked Nov 15 '18 at 11:50
Adam PavlovAdam Pavlov
136111
136111
do you want to uncheck the checkbox after closing the button?
– Kiranramchandran
Nov 15 '18 at 12:21
add a comment |
do you want to uncheck the checkbox after closing the button?
– Kiranramchandran
Nov 15 '18 at 12:21
do you want to uncheck the checkbox after closing the button?
– Kiranramchandran
Nov 15 '18 at 12:21
do you want to uncheck the checkbox after closing the button?
– Kiranramchandran
Nov 15 '18 at 12:21
add a comment |
3 Answers
3
active
oldest
votes
From question i understand that OP need to uncheck the check box after closing the button. So i did the following way.
$('.filter-label li .btn-close span').click(function()
$('input[type=checkbox][id='+$(this).attr('id') +']').prop('checked',false);// Newly added code to uncheck the checkbox.
$(this).closest('li').remove();
);
DEMO HERE
add a comment |
Working fiddle.
Since you're appendin an li
element when the user check, you need to remove the parent li
not the button itself when the user uncheck, the just add .closest('li')
before removing :
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() + ")").filter(function()
return $(this).parent('li').length > 0;
).closest('li').remove();
___^^^^^^^^^^^^^
@Adam could you please tell us why you've accepted the newest answer to your question what is almost a copy of the old answers here?
– Zakaria Acharki
Nov 15 '18 at 13:47
add a comment |
In else
you trying to select button and remove it using .remove()
. So you should select li
parent of button using .parent()
and remove it.
$(".left-menu input[name='freeAsset']").click(function()
if($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">'+ $(this).parent('div').children('span').text() +'<span aria-hidden="true" id="'+this.id+'">×</span></button></li>');
$('.filter-label li .btn-close span').click(function()
$(this).closest('li').remove();
);
else
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() +")").filter(function ()
return $(this).parent('li').length > 0;
).parent().remove();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="left-menu">
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills1">
<label for="Skills1" class="chk"></label>
<span class="txt">Soft Skills</span>
</div>
</li>
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills2">
<label for="Skills2" class="chk"></label>
<span class="txt">Technical Skills</span>
</div>
</li>
</ul>
<ul class="filter-label clearfix search_lable"></ul>
Also you can use simpler code as shown in bottom
$(".left-menu input[name='freeAsset']").click(function()
var text = $(this).parent('div').children('span').text();
if ($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">'+ text +'<span aria-hidden="true" id="'+this.id+'">×</span></button></li>');
else
$(".search_lable li button:contains("+text+")").parent().remove();
);
$(document).on('click', '.filter-label li .btn-close span', function()
$(this).closest('li').remove();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="left-menu">
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills1">
<label for="Skills1" class="chk"></label>
<span class="txt">Soft Skills</span>
</div>
</li>
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills2">
<label for="Skills2" class="chk"></label>
<span class="txt">Technical Skills</span>
</div>
</li>
</ul>
<ul class="filter-label clearfix search_lable"></ul>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318862%2funcheck-checkbox-on-click-button%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
From question i understand that OP need to uncheck the check box after closing the button. So i did the following way.
$('.filter-label li .btn-close span').click(function()
$('input[type=checkbox][id='+$(this).attr('id') +']').prop('checked',false);// Newly added code to uncheck the checkbox.
$(this).closest('li').remove();
);
DEMO HERE
add a comment |
From question i understand that OP need to uncheck the check box after closing the button. So i did the following way.
$('.filter-label li .btn-close span').click(function()
$('input[type=checkbox][id='+$(this).attr('id') +']').prop('checked',false);// Newly added code to uncheck the checkbox.
$(this).closest('li').remove();
);
DEMO HERE
add a comment |
From question i understand that OP need to uncheck the check box after closing the button. So i did the following way.
$('.filter-label li .btn-close span').click(function()
$('input[type=checkbox][id='+$(this).attr('id') +']').prop('checked',false);// Newly added code to uncheck the checkbox.
$(this).closest('li').remove();
);
DEMO HERE
From question i understand that OP need to uncheck the check box after closing the button. So i did the following way.
$('.filter-label li .btn-close span').click(function()
$('input[type=checkbox][id='+$(this).attr('id') +']').prop('checked',false);// Newly added code to uncheck the checkbox.
$(this).closest('li').remove();
);
DEMO HERE
answered Nov 15 '18 at 12:26
KiranramchandranKiranramchandran
1,9901128
1,9901128
add a comment |
add a comment |
Working fiddle.
Since you're appendin an li
element when the user check, you need to remove the parent li
not the button itself when the user uncheck, the just add .closest('li')
before removing :
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() + ")").filter(function()
return $(this).parent('li').length > 0;
).closest('li').remove();
___^^^^^^^^^^^^^
@Adam could you please tell us why you've accepted the newest answer to your question what is almost a copy of the old answers here?
– Zakaria Acharki
Nov 15 '18 at 13:47
add a comment |
Working fiddle.
Since you're appendin an li
element when the user check, you need to remove the parent li
not the button itself when the user uncheck, the just add .closest('li')
before removing :
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() + ")").filter(function()
return $(this).parent('li').length > 0;
).closest('li').remove();
___^^^^^^^^^^^^^
@Adam could you please tell us why you've accepted the newest answer to your question what is almost a copy of the old answers here?
– Zakaria Acharki
Nov 15 '18 at 13:47
add a comment |
Working fiddle.
Since you're appendin an li
element when the user check, you need to remove the parent li
not the button itself when the user uncheck, the just add .closest('li')
before removing :
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() + ")").filter(function()
return $(this).parent('li').length > 0;
).closest('li').remove();
___^^^^^^^^^^^^^
Working fiddle.
Since you're appendin an li
element when the user check, you need to remove the parent li
not the button itself when the user uncheck, the just add .closest('li')
before removing :
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() + ")").filter(function()
return $(this).parent('li').length > 0;
).closest('li').remove();
___^^^^^^^^^^^^^
answered Nov 15 '18 at 12:01
Zakaria AcharkiZakaria Acharki
55.8k134471
55.8k134471
@Adam could you please tell us why you've accepted the newest answer to your question what is almost a copy of the old answers here?
– Zakaria Acharki
Nov 15 '18 at 13:47
add a comment |
@Adam could you please tell us why you've accepted the newest answer to your question what is almost a copy of the old answers here?
– Zakaria Acharki
Nov 15 '18 at 13:47
@Adam could you please tell us why you've accepted the newest answer to your question what is almost a copy of the old answers here?
– Zakaria Acharki
Nov 15 '18 at 13:47
@Adam could you please tell us why you've accepted the newest answer to your question what is almost a copy of the old answers here?
– Zakaria Acharki
Nov 15 '18 at 13:47
add a comment |
In else
you trying to select button and remove it using .remove()
. So you should select li
parent of button using .parent()
and remove it.
$(".left-menu input[name='freeAsset']").click(function()
if($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">'+ $(this).parent('div').children('span').text() +'<span aria-hidden="true" id="'+this.id+'">×</span></button></li>');
$('.filter-label li .btn-close span').click(function()
$(this).closest('li').remove();
);
else
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() +")").filter(function ()
return $(this).parent('li').length > 0;
).parent().remove();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="left-menu">
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills1">
<label for="Skills1" class="chk"></label>
<span class="txt">Soft Skills</span>
</div>
</li>
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills2">
<label for="Skills2" class="chk"></label>
<span class="txt">Technical Skills</span>
</div>
</li>
</ul>
<ul class="filter-label clearfix search_lable"></ul>
Also you can use simpler code as shown in bottom
$(".left-menu input[name='freeAsset']").click(function()
var text = $(this).parent('div').children('span').text();
if ($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">'+ text +'<span aria-hidden="true" id="'+this.id+'">×</span></button></li>');
else
$(".search_lable li button:contains("+text+")").parent().remove();
);
$(document).on('click', '.filter-label li .btn-close span', function()
$(this).closest('li').remove();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="left-menu">
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills1">
<label for="Skills1" class="chk"></label>
<span class="txt">Soft Skills</span>
</div>
</li>
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills2">
<label for="Skills2" class="chk"></label>
<span class="txt">Technical Skills</span>
</div>
</li>
</ul>
<ul class="filter-label clearfix search_lable"></ul>
add a comment |
In else
you trying to select button and remove it using .remove()
. So you should select li
parent of button using .parent()
and remove it.
$(".left-menu input[name='freeAsset']").click(function()
if($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">'+ $(this).parent('div').children('span').text() +'<span aria-hidden="true" id="'+this.id+'">×</span></button></li>');
$('.filter-label li .btn-close span').click(function()
$(this).closest('li').remove();
);
else
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() +")").filter(function ()
return $(this).parent('li').length > 0;
).parent().remove();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="left-menu">
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills1">
<label for="Skills1" class="chk"></label>
<span class="txt">Soft Skills</span>
</div>
</li>
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills2">
<label for="Skills2" class="chk"></label>
<span class="txt">Technical Skills</span>
</div>
</li>
</ul>
<ul class="filter-label clearfix search_lable"></ul>
Also you can use simpler code as shown in bottom
$(".left-menu input[name='freeAsset']").click(function()
var text = $(this).parent('div').children('span').text();
if ($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">'+ text +'<span aria-hidden="true" id="'+this.id+'">×</span></button></li>');
else
$(".search_lable li button:contains("+text+")").parent().remove();
);
$(document).on('click', '.filter-label li .btn-close span', function()
$(this).closest('li').remove();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="left-menu">
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills1">
<label for="Skills1" class="chk"></label>
<span class="txt">Soft Skills</span>
</div>
</li>
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills2">
<label for="Skills2" class="chk"></label>
<span class="txt">Technical Skills</span>
</div>
</li>
</ul>
<ul class="filter-label clearfix search_lable"></ul>
add a comment |
In else
you trying to select button and remove it using .remove()
. So you should select li
parent of button using .parent()
and remove it.
$(".left-menu input[name='freeAsset']").click(function()
if($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">'+ $(this).parent('div').children('span').text() +'<span aria-hidden="true" id="'+this.id+'">×</span></button></li>');
$('.filter-label li .btn-close span').click(function()
$(this).closest('li').remove();
);
else
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() +")").filter(function ()
return $(this).parent('li').length > 0;
).parent().remove();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="left-menu">
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills1">
<label for="Skills1" class="chk"></label>
<span class="txt">Soft Skills</span>
</div>
</li>
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills2">
<label for="Skills2" class="chk"></label>
<span class="txt">Technical Skills</span>
</div>
</li>
</ul>
<ul class="filter-label clearfix search_lable"></ul>
Also you can use simpler code as shown in bottom
$(".left-menu input[name='freeAsset']").click(function()
var text = $(this).parent('div').children('span').text();
if ($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">'+ text +'<span aria-hidden="true" id="'+this.id+'">×</span></button></li>');
else
$(".search_lable li button:contains("+text+")").parent().remove();
);
$(document).on('click', '.filter-label li .btn-close span', function()
$(this).closest('li').remove();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="left-menu">
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills1">
<label for="Skills1" class="chk"></label>
<span class="txt">Soft Skills</span>
</div>
</li>
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills2">
<label for="Skills2" class="chk"></label>
<span class="txt">Technical Skills</span>
</div>
</li>
</ul>
<ul class="filter-label clearfix search_lable"></ul>
In else
you trying to select button and remove it using .remove()
. So you should select li
parent of button using .parent()
and remove it.
$(".left-menu input[name='freeAsset']").click(function()
if($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">'+ $(this).parent('div').children('span').text() +'<span aria-hidden="true" id="'+this.id+'">×</span></button></li>');
$('.filter-label li .btn-close span').click(function()
$(this).closest('li').remove();
);
else
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() +")").filter(function ()
return $(this).parent('li').length > 0;
).parent().remove();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="left-menu">
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills1">
<label for="Skills1" class="chk"></label>
<span class="txt">Soft Skills</span>
</div>
</li>
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills2">
<label for="Skills2" class="chk"></label>
<span class="txt">Technical Skills</span>
</div>
</li>
</ul>
<ul class="filter-label clearfix search_lable"></ul>
Also you can use simpler code as shown in bottom
$(".left-menu input[name='freeAsset']").click(function()
var text = $(this).parent('div').children('span').text();
if ($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">'+ text +'<span aria-hidden="true" id="'+this.id+'">×</span></button></li>');
else
$(".search_lable li button:contains("+text+")").parent().remove();
);
$(document).on('click', '.filter-label li .btn-close span', function()
$(this).closest('li').remove();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="left-menu">
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills1">
<label for="Skills1" class="chk"></label>
<span class="txt">Soft Skills</span>
</div>
</li>
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills2">
<label for="Skills2" class="chk"></label>
<span class="txt">Technical Skills</span>
</div>
</li>
</ul>
<ul class="filter-label clearfix search_lable"></ul>
$(".left-menu input[name='freeAsset']").click(function()
if($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">'+ $(this).parent('div').children('span').text() +'<span aria-hidden="true" id="'+this.id+'">×</span></button></li>');
$('.filter-label li .btn-close span').click(function()
$(this).closest('li').remove();
);
else
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() +")").filter(function ()
return $(this).parent('li').length > 0;
).parent().remove();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="left-menu">
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills1">
<label for="Skills1" class="chk"></label>
<span class="txt">Soft Skills</span>
</div>
</li>
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills2">
<label for="Skills2" class="chk"></label>
<span class="txt">Technical Skills</span>
</div>
</li>
</ul>
<ul class="filter-label clearfix search_lable"></ul>
$(".left-menu input[name='freeAsset']").click(function()
if($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">'+ $(this).parent('div').children('span').text() +'<span aria-hidden="true" id="'+this.id+'">×</span></button></li>');
$('.filter-label li .btn-close span').click(function()
$(this).closest('li').remove();
);
else
$(".search_lable").find("button:contains(" + $(this).parent('div').children('span').text() +")").filter(function ()
return $(this).parent('li').length > 0;
).parent().remove();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="left-menu">
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills1">
<label for="Skills1" class="chk"></label>
<span class="txt">Soft Skills</span>
</div>
</li>
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills2">
<label for="Skills2" class="chk"></label>
<span class="txt">Technical Skills</span>
</div>
</li>
</ul>
<ul class="filter-label clearfix search_lable"></ul>
$(".left-menu input[name='freeAsset']").click(function()
var text = $(this).parent('div').children('span').text();
if ($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">'+ text +'<span aria-hidden="true" id="'+this.id+'">×</span></button></li>');
else
$(".search_lable li button:contains("+text+")").parent().remove();
);
$(document).on('click', '.filter-label li .btn-close span', function()
$(this).closest('li').remove();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="left-menu">
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills1">
<label for="Skills1" class="chk"></label>
<span class="txt">Soft Skills</span>
</div>
</li>
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills2">
<label for="Skills2" class="chk"></label>
<span class="txt">Technical Skills</span>
</div>
</li>
</ul>
<ul class="filter-label clearfix search_lable"></ul>
$(".left-menu input[name='freeAsset']").click(function()
var text = $(this).parent('div').children('span').text();
if ($(this).is(':checked'))
$('.search_lable').append('<li><button type="button" class="btn-close image" aria-label="Close" style="display: inline-block;">'+ text +'<span aria-hidden="true" id="'+this.id+'">×</span></button></li>');
else
$(".search_lable li button:contains("+text+")").parent().remove();
);
$(document).on('click', '.filter-label li .btn-close span', function()
$(this).closest('li').remove();
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="left-menu">
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills1">
<label for="Skills1" class="chk"></label>
<span class="txt">Soft Skills</span>
</div>
</li>
<li>
<div class="label-cont clearfix">
<input type="checkbox" name="freeAsset" value="valuable" id="Skills2">
<label for="Skills2" class="chk"></label>
<span class="txt">Technical Skills</span>
</div>
</li>
</ul>
<ul class="filter-label clearfix search_lable"></ul>
edited Nov 15 '18 at 12:14
answered Nov 15 '18 at 12:01
MohammadMohammad
15.8k123764
15.8k123764
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318862%2funcheck-checkbox-on-click-button%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
do you want to uncheck the checkbox after closing the button?
– Kiranramchandran
Nov 15 '18 at 12:21