1

I'm creating a prototype application. I wanted to template my header and footer over html files. I'm loading these in using jQuery load.

In my index file I have this in the first part of my body:

<!--COMMON HEADER ------------- -->
        <header id="common-include-header" class="blade child ns">
        </header>

I'm including via a document called includes.js which is listed before header.js in my head/script tags. Here is the includes.js like so:

$(document).ready(function () {
    $("#common-include-header").load("includes/header.html");
    $("#common-include-footer").load("includes/footer.html");
});

Here is my header.html:

<div class="blade-content flexr xc sb">

    <img src="img/logo100.svg"></img>

    <div class="child g">
        <form class="search-form flexr jc">
            <input class="location" value="Auckland" />
            <input class="context" placeholder="Find something" />
            <button class="child ns"><i class="fa fa-search"></i></button>
        </form>
    </div>

    <button class="search-toggle"><i class="fa fa-search"></i></button>

    <div class="child ns">
        <button class="sign-in-button pill outline pink">Sign in</button>

        <button class="user-signed-in flexr xc hide">
            <p>[email protected]</p>
            <img src="img/av.png"></img>
            <i class="fa fa-chevron-down"></i>
        </button>
    </div>

    <nav class="signed-in-nav flexc">
        <button type="button" class="dropdown-menu child ns">Favourites</button>
        <button type="button" class="dropdown-menu child ns">Reviews</button>
        <button type="button" class="dropdown-menu child ns">Businesses</button>
        <button type="button" class="dropdown-menu sign-out child ns">Sign out</button>
    </nav>

</div>

And my header.js

$(document).ready(function () {

    //hide/show mobile search
    $(".search-toggle").on("click", function () {
        if ($(".toggle-search").hasClass('open')) {
            $(".toggle-search").removeClass("open");
        } else {
            $(".toggle-search").addClass("open");
        };
    });

    //hide/show sign in
    $(".sign-in-button").on("click", function () {
        $(".sign-in-button").addClass("hide");
        $(".user-signed-in").removeClass("hide");
    });

    //hide/show user menu
    $(".user-signed-in").on("click", function () {
        if ($(".signed-in-nav").hasClass("open")) {
            $(".signed-in-nav").removeClass("open");
            $(".user-signed-in").removeClass("open");
        } else {
            $(".signed-in-nav").addClass("open");
            $(".user-signed-in").addClass("open");
        };
    });

    //sign out
    $("button.sign-out").on("click", function () {
        $(".signed-in-nav").removeClass("open");
        $(".user-signed-in").removeClass("open");
        $(".user-signed-in").addClass("hide");
        $(".sign-in-button").removeClass("hide");
    });

});

My problem is that the event handlers aren't binding to .sign-in-button. I would have thought since I used jQuery .on this would work. I can't figure it out. Where am I going wrong?

4
  • 2
    @MKA is right! Read about event delegation Commented Dec 7, 2015 at 4:40
  • What is purpose of not loading all html at index.html ? Commented Dec 7, 2015 at 4:41
  • 1
    @guest271314 because I have about 20 files with consistent headers and footers, I'm loading them in so I have one code base for the header and footer - it's a prototype that will be templated into an MVC framework later. Just trying to cut down on needing to update multiple header tags if something changes (which it will). Commented Dec 7, 2015 at 4:50
  • @MKA yep, didn't work. Callback answer worked below. Commented Dec 7, 2015 at 4:58

2 Answers 2

2

jQuery's load has a callback function...

Try this:

$("#common-include-header").load("includes/header.html", function(){
  // bind to header code here
});
$("#common-include-footer").load("includes/footer.html", function(){
  // bind to any footer code here
});

You can also use .toggleClass() instead of having separate lines to add or remove class names.

//hide/show mobile search
$(".search-toggle").on("click", function () {
    var isOpen = $(".toggle-search").hasClass('open');
    $(".toggle-search").toggleClass("open", !isOpen);
});

//hide/show sign in
$(".sign-in-button").on("click", function () {
    $(".sign-in-button").addClass("hide");
    $(".user-signed-in").removeClass("hide");
});

//hide/show user menu
$(".user-signed-in").on("click", function () {
    var isOpen = $(".signed-in-nav").hasClass("open");
    $(".signed-in-nav").toggleClass("open", !isOpen);
    $(".user-signed-in").toggleClass("open", !isOpen);
});

//sign out
$("button.sign-out").on("click", function () {
    $(".signed-in-nav, .user-signed-in").removeClass("open");
    $(".user-signed-in").addClass("hide");
    $(".sign-in-button").removeClass("hide");
});
Sign up to request clarification or add additional context in comments.

1 Comment

OP has different js files for each template..Your solution may not be helpful in that case!
0

As you are loading your html on DOM ready, all your javascript files are already loaded and your header.html is being loaded after that. So your header.js can not locate the element using $(".search-toggle").on("click", function () { . Here you have to use $(document).on("click",".search-toggle", function () {

Comments

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.