1

I have a menu and my users should be able to resize it by pressing a button. I have the following code:

<header class='large'>
    <nav>
        <ul>
            <li><a href="#">Link 1</a></li>
            <li><a href="#">Link 2</a></li>
            <li><a href="#">Link 3</a></li>
        </ul>
    </nav>
</header>
<button onclick="resizeMenu()">Resize</button>




function resizeMenu()
    {
        if(exists(header.large) {
            $("header").removeClass("large").addClass("small");
        } elseif(exists(header.small)
            $("header").removeClass("small").addClass("large");
        }
    }

I don't really have an idea on how I should write the condition. By the way I am using jquery.

4 Answers 4

3

You can use the .hasClass from jQuery:

function resizeMenu()
{
    if($("header").hasClass("large")) {
        $("header").removeClass("large").addClass("small");
    } elseif($("header").hasClass("small")) 
        $("header").removeClass("small").addClass("large");
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I prefer the answer by Arun, but +1 for this since it tells him how to check an element for a class.
You are right. In his situation, .toggle() would be better. But in the case where he would want small, medium, large, xx-large, etc.. toggle wont do the trick.
2

Use toggleClass() to switch between the 2 classes

function resizeMenu() {
    $("header").toggleClass("large small");
}

To check whether an element has class use .hasClass()

function resizeMenu() {
    var $header = $('header')
    if ($header.hasClass('large')) {
        $header.removeClass("large").addClass("small");
    } else if ($header.hasClass('small')) {
        $header.removeClass("small").addClass("large");
    }
}

Comments

0

is this you want?

<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
    $(document).ready(function () {
        $("#resize").click(function () {
            $("nav").animate({
                height: ["toggle", "swing"]
            }, {
                duration: 500,
                complete: function () {

                }
            });

        });
    });

</script>
</head>
<body>
<header>
<nav>
    <ul>
        <li><a href="#">Link 1</a></li>
        <li><a href="#">Link 2</a></li>
        <li><a href="#">Link 3</a></li>
    </ul>
</nav>
</header>
<button id="resize">
    Resize</button>
</body>
</html>

Comments

0

Simply Write. Use Toggle Class in Jquery for add and remove Class

function resizeMenu()
{

   $("header").toggleClass("large small");

}

1 Comment

Please read other answers before posting. The .toggleClass() method was already covered by Arun P Johny 15 minutes ago.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.