user9371102
user9371102

Reputation: 1276

Hide last div in a page using jquery

I need to hide last div from my web page.

("div").eq(-1).hide();

Can I have the code for working

Thanks in advance, Vicky

Upvotes: 0

Views: 864

Answers (5)

Musa
Musa

Reputation: 97672

Try

$("div:last").hide();

or

$("div").last().hide();

I didn't know eq took negative indexes so your only problem is that you were missing the $/jQuery before your code.

Upvotes: 4

Shibu Thomas
Shibu Thomas

Reputation: 3196

First reference jQuery. Add the code inside $(document).ready(). A page can't be manipulated safely until the document is ready. jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

$(document).ready(function () {
    $("div").eq(-1).hide();
});   

Upvotes: 0

user1596371
user1596371

Reputation:

This will do the trick:

$("div").last().hide()

Upvotes: 1

GolezTrol
GolezTrol

Reputation: 116110

I think the problem is the missing $ at the start of the line.

But you can also use last, which I think is more readable. It's as easy as:

$("div").last().hide();

Upvotes: 2

Fabrício Matté
Fabrício Matté

Reputation: 70149

You're not referencing jQuery at all:

  $("div").eq(-1).hide();
//^-- missing the $ for jQuery

You can also use a more semantic approach:

$("div:last").hide();

Fiddle1 Fiddle2

Upvotes: 4

Related Questions