4

I have a menu button, which when clicked will slide down a div below.

My menu is title "+ Open menu"

When the menu button is clicked my code changes the title to "+ Menu" to indicate that the menu is open.

When the user closes the menu I would like the title to revert back to "- Menu"

This is my HTML which defines the class, test in this case.

<span class="test">+ Open menu</span>

Here is my jQuery function, I cannot figure out how to revert the menu state.

$(document).ready(function () {
$(".widget_head_type").click(function () 
{
    $(".widget_body_type").slideToggle("slow");
    $(".test").text("- Close menu");
});

});

BallSoHard
  • 93
  • 1
  • 4
  • 9

5 Answers5

12
$(".widget_head_type").click(function () {
    $(".widget_body_type").slideToggle("slow");
    ($(".test").text() === "+ Open menu") ? $(".test").text("- Close menu") : $(".test").text("+ Open menu");
});
Bitter
  • 156
  • 1
  • 6
1
jQuery.fn.extend({
    toggleText: function (a, b){
        var that = this;
            if (that.text() != a && that.text() != b){
                that.text(a);
            }
            else
            if (that.text() == a){
                that.text(b);
            }
            else
            if (that.text() == b){
                that.text(a);
            }
        return this;
    }
});

Use as:

$("#YourElementId").toggleText('After', 'Before');
Diego Favero
  • 1,969
  • 2
  • 22
  • 32
  • although one liner solution would be this `$.fn.extend({ toggleText: function(a, b){ return this.text(this.text() == b ? a : b); } });` not exactly one line, i mean. usage is same like yours `$(".example").toggleText('Initial', 'Secondary');` – Danish Mar 14 '17 at 09:01
1

Here is an interesting one based on the http://api.jquery.com/toggle/

HTML

<span class="test">+ Open menu</span>
<span class="test" style="display:none">- Close menu</span>

JS

$(document).ready(function () {
$(".widget_head_type").click(function () 
{
    $(".widget_body_type").slideToggle("slow");
    $(".test").toggle();
});

Simple, just toggles the all tags with class test whether hidden or visible.

user3257693
  • 486
  • 3
  • 14
0

WORKING FIDDLE HERE

I've guessed at your html, and made some notes for alternatives, but you consider the following:

HTML:

<div class="some_parent_class">
    <div class="widget_head_type">
        <span class="test">+ Menu</span>
    </div>
    <div class="widget_body_type">
        Body
    </div>
</div>

JS:

jQuery(function ($) {
    $('.widget_head_type').each(function () {
        // closures
        var $toggle = $(this);
        var $parent = $toggle.closest('.some_parent_class');
        var $target = $parent.find('.widget_body_type');
        var $label = $toggle.find('.test');
        var bIsTweening = false;
        // OR var $target = $toggle.next('.widget_body_type');
        // event methods (closures)
        var fClickToggle = function () {
            if (!bIsTweening) {
                bIsTweening = true;
                $target.slideToggle('slow', fToggleCallback);
            }
        };
        var fToggleCallback = function () {
            bIsTweening = false;
            fSetLabel();
        };
        var fSetLabel = function () {
            var sLabel = $label.text().replace('+', '').replace('-', '');
            sLabel = ($target.is(':visible') ? '-' : '+') + sLabel;
            $label.html(sLabel);
        };
        // handle event
        $toggle.click(fClickToggle);
        $target.hide();
        fSetLabel();
    });
});
Levi Beckman
  • 533
  • 3
  • 8
0

had same issue and this is how i solved it: using your code example.

$(".widget_head_type").click(function () {
    $(".widget_body_type").slideToggle("slow");
    ($(".test").text() == "+ Open menu") ? $(".test").text("- Close menu") : $(".test").text("+ Open menu");
});

use two equal signs for comparison.

Marcel Burkhard
  • 3,453
  • 1
  • 29
  • 35
Ossaija D
  • 67
  • 2