$('.pagination ul li').each({
if( $(this).index(this) > 2 )
{
$(this).hide();
}
});
SyntaxError: missing : after property id whats the problem?
$('.pagination ul li').each({
if( $(this).index(this) > 2 )
{
$(this).hide();
}
});
SyntaxError: missing : after property id whats the problem?
Share Improve this question asked Sep 5, 2012 at 15:48 DenasDenas 411 gold badge1 silver badge3 bronze badges 4 |2 Answers
Reset to default 12Use the keyword function
preceding {}
or it will be interpreted as an object literal.
$('.pagination ul li').each(function() {
if ($(this).index(this) > 2) {
$(this).hide();
}
});
Also, $(this).index(this)
does not do what you expect. Did you want to check if the index at which the element is located is greater than 2? Use this revision instead:
$('.pagination ul li').each(function(idx) {
if (idx > 2) {
$(this).hide();
}
});
You need to pass .each
a function
. Without the function()
, it's being read as an object ({}
).
$('.pagination ul li').each(function(){
if($(this).index(this) > 2){
$(this).hide();
}
});
P.S. $(this).index(this)
does not do what you think it does. It will search inside this
for this
, therefore it always returns 0
.
If you want the index of the li
in the ul
, use the index parameter from the .each
.
$('.pagination ul li').each(function(index){
if(index > 2){
$(this).hide();
}
});
P.P.S. If all you want to do is hide the li
s that have an index > 2
, then you can do this even easier:
$('.pagination ul li:gt(2)').hide();
$(this).index(this)
will get what you want. – gen_Eric Commented Sep 5, 2012 at 15:50