General Question

richardhenry's avatar

Scriptaculous effect on Element.insert?

Asked by richardhenry (12692points) March 22nd, 2008

Is it possible to have a Scriptaculous effect occur when you apply an Element.insert? (in regards to Prototype and Scriptaculous)

—-

Snippet of my code:
$(‘activity’).insert({ top: transport.responseText });

—-

Can I have an effect like highlight or blind down occur to the inserted content?

Any ideas? Thanks :D

Observing members: 0 Composing members: 0

4 Answers

adrianscott's avatar

Assuming that you only want to highlight or blind down (or any other effect that you desire) only the content you’ve inserted and not the whole ‘activity’ object, then I think you’ll be faced with 2 different answers depending on what you want to do.

In the case of Highlight, I don’t think it’s requisite that you have a block element (I could be wrong, however) so what I would do is wrap your response text within a span tag, perhaps with a randomly generated id, and call Effect.Highlight after the insert:

$(‘activity’).insert({ top: ’<span id=“newText”>’+transport.responseText+’</span>’ });
new Effect.Highlight(‘newText’);

If you want to do a blind down, slide down, etc. you’re going to need to place your new text within a block element (I’d use a div). The downside to this approach is that if the new content you’re loading into the page isn’t an entirely new paragraph, it’s not going to look too pretty:

$(‘activity’).insert({ top: ’<div id=“newText” style=“display:none;”>’+transport.responseText+’</div>’ });
new Effect.BlindDown(‘newText’);

Hope that helped!

richardhenry's avatar

Very much so, thanks! In the case of what I’m doing, multiple updates would be made, so I simply ran $(‘activity’).id = null; in order to clear the ID for use by other elements.

Is there any better way to do this? Thanks

adrianscott's avatar

You could use the JavaScript Math library to help you produce some randomized id’s so that every time the function is called, it (should!) have a unique identifier.

var rand_id = ‘newText_’+Math.floor(Math.random() * 10000 + 1);
$(‘activity’).insert({ top: ’<span id=”’+rand_id+’”>’+transport.responseText+’</span>’ });
new Effect.Highlight(rand_id);

Alternatively, you could keep a global counter to increment the integer value by 1 on every call to your function so that every id is unique…

richardhenry's avatar

A global counter is probably the best way to do it, as although it’s unlikely, a random value would have the possibility of being the same as another produced result. Cheers again!

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther