EnexoOnoma
EnexoOnoma

Reputation: 8836

jQuery script takes me on top of page. How to modify this?

How to modify this code so that when I click on it, it will not take me to the top of the page?

<script>
    $(document).ready(function () {
        $(".cont").click(function () {
            $("#feed").load('footer.php');
        });
    });
</script>

Upvotes: 0

Views: 158

Answers (4)

Danil Speransky
Danil Speransky

Reputation: 30453

Use:

$(document).ready(function () {
    $(".cont").click(function (e) {
        e.preventDefault();
        $("#feed").load('footer.php');
    });
});

Or in href write something like javascript:void(0);

Upvotes: 0

Quentin
Quentin

Reputation: 943564

The code you have shared with us won't take you to the top of the page.

You might have something like this in addition:

<a href="#" class="cont">

That takes you to the top of the page because that is where the URI # points.

Have a more sensible URI. Possibly:

<a href="footer.php" class="cont">

Then stop the default behaviour in the script:

    $(".cont").click(function (e) {
        $("#feed").load(this.href);
        e.preventDefault();
    });

Upvotes: 0

Alexander Larikov
Alexander Larikov

Reputation: 2339

By adding return false or e.preventDefault()

<script>
    $(document).ready(function () {
        $(".cont").click(function (e) {
            e.preventDefault();
            $("#feed").load('footer.php');
        });
    });
</script>

Upvotes: 2

Robert K
Robert K

Reputation: 30328

You need to listen to the event an prevent its default action.

$(document).ready(function () {
    $(".cont").click(function (e) {
        e.preventDefault();
        $("#feed").load('footer.php');
    });
});

Upvotes: 3

Related Questions