-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 ?

0

3 Answers 3

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); 
Sign up to request clarification or add additional context in comments.

3 Comments

is it really that simple? :p thanks for your answer and suggestion.I will accept your answer as soon as I can.
@Selman22: Selectors are just strings. jQuery doesn't care how the string is created :)
@FelixKling yeah I noticed that.I'm really noob at jQuery :) that was a really simple one.
1

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

Try,

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

Comments

1

You can concatenate your selector with variable using +:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.