tags:

views:

250

answers:

6

I'm showing tooltips like this

$(clickedElement).append('<span id="activeHelpTip" class="helpTip">Loading help...</span>');
$('#activeHelpTip').remove();

A colleague suggested that on page load I append ALL possible tooltips with visibility none, and show/hide them on demand.

What's the difference? Who cares?

Edit: Is it relevant that tooltips are provided by an ajax service?

+1  A: 

I'd say it's somewhat easier and more maintainable to do it the way your colleague is talking about, because that usually results in the tooltips being next to the elements they refer to, in the HTML, instead of off in a script somewhere, and you're writing them directly in HTML instead of inside JS strings.

I don't anticipate that either is going to be perceptibly faster than the other, or use a meaningfully different amount of memory.

Re your edit: If your tooltips are currently being pulled via Ajax, then naturally they'll come up much faster if they were preloaded in the page instead.

chaos
as is the tooltip is next to the element in rendered HTML. Are you suggesting I go into the HTML and c/p `<span class="helpTip"...` 30 times?
Dustin Getz
Doesn't each element have its own tooltip text? If you have 30 elements that all use the same tooltip, of course you shouldn't make 30 separate repetitive tooltip spans. I'd probably stay with what you're doing if that's the case, just because it's already working, but if you really wanted to go to the show/hide approach, you could put a single common tooltip span for all 30 elements at, say, the top of some section that contains the 30 elements, and reposition the span to the cursor when you show it.
chaos
A: 

That is just a much cleaner and easier way to do it. Also, you could use different methods to show/hide the tips like fadeIn()/fadeOut(), slideDown()/slideUp on the hidden element.

Technowise
help me understand how it would be cleaner; the only reason im hesitant to change is that this way seems way simpler.
Dustin Getz
You did not mention you had 30 other elements like that. In that case, I would suggest using an absolute tool-tip element and set that element to the cursor position before show/hide (as chaos already suggested).
Technowise
A: 

Since you're already using jQuery, you should check out the Tooltip plugin. It makes showing tooltips pretty straightforward and is very flexible.

Nate Kohari
+1  A: 

Maintainability aside - what you are doing will perform well, following best practices for tight scoping of selectors. So there is no performance reason to change.

From a maintainability perspective its definitely could be improved to make the list of tips more editable - getting the text away from the mark up.

Rob Fuller
Is it relevant that tooltips are provided by an ajax service?
Dustin Getz
Not really - your selector still narrowly injects at the right point and is well written. I'd probably wire up the ajax call postback to append the content onto the selected element.
Rob Fuller
+1  A: 

The biggest reason I could think to use show and hide instead of append and remove is that when you use .remove() it will destroy all event handlers and cached $.data on the DOM Objects you are removing.

From jQuery docs

Removes all matched elements from the DOM. This does NOT remove them from the jQuery object, allowing you to use the matched elements further. Note that this function starting with 1.2.2 will also remove all event handlers and internally cached data. So:

gnarf
A: 

use hide/show to show parts of form with validation. have a problem. hiden part of form still need to be validated. suxx. hope append/remove will help me.

serhiy