-3

In my MVC project I want to refresh my page with new entries using AJAX.Everything is working great but I want to add some effect when displaying the new entries. First I use prepend like this:

$(".mCSB_container").prepend(data.htmlContent);

Then is I do this:

$(".entry:lt(4)").hide().fadeIn(3000);

It is working correctly.But, I would like to do it with a dynamic variable.I'm returning the new entry count from my Controller Action and set it like this:

 var count = data.count;

And when I want to use it like:

 $(".entry:lt(count)").hide().fadeIn(3000);

Visual studio show me the error message that says: Expected <integer>.I tried this when I defining the count:

 var count = new Number(data.count);

But that doesn't make any difference.Basically I want to select first N items with a given class, then hide and display them with fadeIn. How can I do it if the element count is dynamic ? Is it possible with :lt selector or is there another way I can use ?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Selman Genç
  • 100,147
  • 13
  • 119
  • 184

3 Answers3

2

String concatenation is what you are looking for

$(".entry:lt(" + count + ")").hide().fadeIn(3000);

but a faster alternate is to use .slice()

$(".entry").slice(0, count).hide().fadeIn(3000);
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

The way you have concatenated the variable with the selector is wrong.

Try,

$(".entry:lt(" + count + ")").hide().fadeIn(3000);
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
1

You can concatenate your selector with variable using +:

$(".entry:lt(" + count + ")").hide().fadeIn(3000);
Felix
  • 37,892
  • 8
  • 43
  • 55