I have a simple script in jquery to toggle a div (show and hide) when a <p>
is clicked (I'm using bootstrap).
HTML:
<p id="click_advance"><i class="icon-circle-arrow-down"></i> Advanced search</p>
<div id="display_advance">
<p>This is the advance search</p>
</div>
JS:
$('#click_advance').click(function(){
$('#display_advance').toggle('1000');
$(this).html('<i class="icon-circle-arrow-up"></i> Advanced search');
});
So, when I click for the first time the icon changes from down to up but obviously when I click "click_advance" again the icon doesn't change back. So I want the toggle effect like the show and hide; but I'm cluless on how to do it with an icon.
If your icon is based on the text in the block (ligatures) rather the class of the block then the following will work. This example uses the Google Material Icons '+' and '-' icons as part of MaterializeCSS.
Edit: Added missing
);
needed for this to function properly.It also works for JQuery post 1.9 where toggling of functions was deprecated.
If .toggle is not working I would do the next:
It's a little bit more code, but it works
Instead of overwriting the html every time, just toggle the class.
Try this:
or even better, as Kevin said:
Here is a very easy way of doing that
Hope this helps :D