2

i want to show and hide the content of my website with jquery. if i click at a link at the navigation, the content should show the <section> with a special id. here my code:

The navigation:

$(document).ready(function(){
  $("a").click(function(){
    $("section#home").css({"display":"none"});
    $("section#order").css({"display":"none"});
    $("section#projects").css({"display":"none"});
    $("section#contact").css({"display":"none"});
    $("section#about").css({"display":"none"});

    if ($(this[href="home"])){
        $("section#home").css({"display":"block"});
    } else if ($(this[href="order"])){
        $("section#order").css({"display":"block"});
    } else if ($(this[href="projects"])){
        $("section#projects").css({"display":"block"});
    } else if ($(this[href="contact"])){
        $("section#contact").css({"display":"block"});
    } else if ($(this[href="about"])){
        $("section#about").css({"display":"block"});
    }
  });
});

The html-file:

<section id="content">
                <section id="home">
                    <h1>home</h1>
                </section>
                <section id="order">
                    <h1>order</h1>
                </section>
                <section id="projects">
                    <h1>projects</h1>
                </section>
                <section id="contact">
                    <h1>contact</h1>
                </section>
                <section id="about">
                    <h1>about</h1>
                </section>
</section>

It shows at every navigation-link the same page (home). What is wrong? you can look by your self at Homepage

2 Answers 2

5

Your syntax in the if condition is wrong, it should be $(this).is('[href="home"]')

$(document).ready(function () {
    $("a").click(function (e) {
        e.preventDefault();

        //hide all sections under #content
        $('#content > section').hide();

        //show the elemet with the given href as the id
        $('#' + $(this).attr('href')).show();

    });
});
Sign up to request clarification or add additional context in comments.

Comments

3

You have to hide all the children section inside the section with id content. Then just get the href attribute from the clicked anchor tag. And show the section which is having the id as same as the clicked anchor tag.

Try,

$(document).ready(function(){
  $("a").click(function(e){
    e.preventDefault();       
    $('#content').children('section').hide();    
    $('#' + $(this).attr('href')).show();
  });
});

5 Comments

dont work..look at link, your code is uploaded, but...dont work ;(
@Tekkzz remove the concatenated # from the selector. See my updated Code.
what does e.preventDefault() do?
@Tekkzz You just changed your markup by removing that # in the href. Now your page will be reloaded when you click on it. So in order to prevent it you should use e.preventDefault(). Technically it will prevent the default behaviour.
ahh, thnak you for this info

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.