How to toggle button on click?
Hi everyone!
The issue I face is that I have identical "button-checkboxes", but once of of them is clicked, I only want the closest private class to change.
- <span class="button-checkbox">
- <button type="button" onclick="privateClick()" class="btn btn-sm btn-default">
- <i class="fa fa-square-o private"></i>
- <button>
- <input name="private" type="checkbox" class="hidden" />
- </span>
- <span class="button-checkbox">
- <button type="button" onclick="privateClick()" class="btn btn-sm btn-default">
- <i class="fa fa-square-o private"></i>
- <button>
- <input name="private" type="checkbox" class="hidden" />
- </span>
I currently use:
- function privateClick()
- {
- $(".private").toggleClass('fa-square-o fa-check-square-o');
- }
to toggle the "checkbox button". But, obviously, if there is more then one button-checkbox, all of them toggle. So I have been trying with:
- function privateClick()
- {
- $(this).find('i').toggleClass('fa-square-o fa-check-square-o');
- }
Unfortunately, the class does not get toggled for buttons that were created through JavaScript after the page was loaded. I am modifying this script:
https://blueimp.github.io/jQuery-File-Upload/ and it works the same. Once some one drags n drops a file into the browser, a new button will get created.
Any help would be greatly appreciated!