Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6616939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:38:37+00:00 2026-05-25T20:38:37+00:00

My intent is to add a class to the header when a div is

  • 0

My intent is to add a class to the header when a div is clicked. I have included the website I’m working with, just to make thing’s easier:

URL – http://itsmontoya.com/work/iM/

I have added a class ‘expanded’ to the header. This is to show how the navigation should look after the button has been pressed. I created a simple Javascript which is supposed to provide an alert when I click the button. I can’t seem to get this to work. Any ideas of what I did wrong?

Thanks!

EDIT – I was able to get the alert to properly work when clicking the button div. I’m very close to having this complete! 🙂 Now I’m getting stuck with the variable not passing correctly.

<script type= "text/javascript">
var expanded = false;
function btnClick(){
    alert('The variable expanded is '+expanded);
    if(expanded === false) {
        document.getElementById("header").className = "expanded";
        var expanded = true;
    } else {
        document.getElementById("header").className.replace(/\bexpanded\b/,'');
        var expanded = false;
    }
};
</script>

I’m updating the ftp server now 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:38:38+00:00Added an answer on May 25, 2026 at 8:38 pm

    When using jQuery, you have to bind your events in such a way that the elements have already loaded.

    You have:

    <script type= "text/javascript">
    $("#expandBtn").click(function(){
       alert('hello');
    });
    </script>
    

    I think what you want is:

    <script type= "text/javascript">
    $(document).ready(function(){
      $("#expandBtn").click(function(){
         alert('hello');
         $('header').addClass('expanded');
      });
    });
    </script>
    

    The API documentation is going to be your friend here. First step — ready().

    UPDATE

    You have this call to jQuery:

    $j('#header').addClass('expanded');
    

    But your markup is for the HTML5 element <header>. In that case your jQuery needs to change to:

    $j('header').addClass('expanded');
    

    Where $j is your jQuery object. More typically you would use $ or jQuery.

    Time to bone up on jQuery Selectors!

    UPDATE

    Here’s your updated page:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>itsMontoya</title>
        <link rel="stylesheet" type="text/css" href="style.css" />
        <link rel="shortcut icon" href="/favicon.ico" />
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
        <script type= "text/javascript">
        $(document).ready(function(){
            $('.expandBtn').bind('click', function(){
                $('header').toggleClass('expanded');
            });
        });
        </script>
    </head>
    <body>
        <div class="wrap">
            <header id="header" class="">
                <div class="blackBG transparent"></div>
                <nav>
                    <ul>
                        <li>
                            Home<img src="images/home.png">
                        </li>
                        <li>
                            Pictures<img src="images/pictures.png">
                        </li>
                        <li>
                            Music<img src="images/mymusic.png">
                        </li>
                        <li>
                            About Me<img src="images/aboutme.png">
                            </li>
                        <li>
                            Resume<img src="images/resume.png">
                        </li>
                    </ul>
                </nav>
                <div id="logo" class="logo"><p>itsMontoya</p></div><div id="expandBtn" class="expandBtn anchor"></div>
            </header>
            <section class="content">
                <article class="blogEntry"></article>
            </section>
            <footer class="anchor">
                <div class="over anchor"><p>2011 itsMontoya.com</p></div>
                <div class="blackBG transparent anchor under"></div>
            </footer>
        </div>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that opens another class using intent : private void createRepository(){
I have an application that calls AlarmManager Intent intent; intent = new Intent(context, MyEventReceiver.class);
I have an Activity SaveData.class with a public method addEvent() use to add some
I have a MainActivity class with an Add Item button and a listview and
The intent here is to deal with obfuscated passwords for resources. We have an
I have a intent receiver in my android manifest, but would like to give
I have a question about parcelable class (android). Until now, I have passed a
I have created TabActivity class in which i am creating four tabs for my
I have used the same way of Zxing Intent to open scanner from my
I new to app development and I have been working on a Flash card

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.