I want text in the header of my jQuery accordion to toggle between "edit" and "close" -- I can get it working when toggling a single panel open and closed, however if I leave a panel open and click on a different panel, the previous panel closes but the text still says "close."
I'm still learning jQuery and JavaScript. I've tried for hours, but can't seem to find a solution. Any suggestions would be hugely appreciated!
This jQuery allows me to toggle the text successfully on one panel:
$(function(){
$(".details-toggle").click(function () {
$(this).find(".edit-toggle").text(function(i, text){
return text === "edit" ? "close" : "edit";
})
});
JSFIDDLE demo: http://jsfiddle.net/dylanstewart/nvhsyx6n/
The function that you need is this:
I Hope this will help you
UPDATE
Just set all the toggles to "edit" whenever one is clicked, then use the
ui-state-active
class to set the text on the activated header, as follows:See the full jsfiddle.
DEMO
Set all
.edit-toggle
elements within theaccordion
to 'edit' first as in the following:NOTE: You don't want to change
.edit-toggle
elements globally;.closest() ... find()
will keep you within the accordion where theclick
originated.Save previous text, then change everything to edit, switch back text and change it. Fiddle is here
Both Jozef Dúc's and mattingly890's solutions work perfect. If you're not using the jQuery UI CSS framework Jozef Dúc's solution is ideal. If you're using the jQuery UI CSS framework then mattingly890's solution is ideal because it requires less code by utilizing the .ui-state-active class. Both effective solutions for different scenarios.
If you're using the jQuery UI CSS framework with accordion use mattingly890's solution:
If you're not using the jQuery UI CSS framework with accordion use Jozef Dúc's solution:
Thanks for these solutions!