Reputation: 1276
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
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
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
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
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();
Upvotes: 4