jQuery function only firing once on button click
I'm having an issue with my function only triggering in one cycle. When I click the Highlight button my text highlights, when I click Un-Highlight the highlighting is removed...however everything stops after this. When I click the Highlight button again nothing happens.
- <script>
- // Use Strict indicates that the code should be executed in "strict mode" which does not allow for use of undeclared variables.
- "use strict";
-
- $(document).ready(function() {
- //Select button and set click function
- $("#toggleHighlight").click(function (e) {
- //if button text = Highlight
- if ($(this).text() == "Highlight") {
- //assign function only to wording tagged class="hl"
- $('span.hl').each(function() {
- var toggle=$(this);
- toggle.removeClass('h1');
- toggle.addClass('highlightable');
- $(this).before(toggle);
- $("#toggleHighlight").text("Unnnn-Highlight");
- });
- }
- else
- {
- //Remove highlighted text - revert button text
- $('span.highlightable').remove();
- $("#toggleHighlight").text("Highlight");
- }
- });
- }); // end ready
- </script>