tags:

views:

67

answers:

2

I'm trying to apply a tooltip to a foreach loop, but I need the javascript to read the attribute "mystickytooltip" with a number at the end.

<div id="mystickytooltip{$smarty.foreach.cart.iteration}" class="stickytooltip">

Will output

<div id="mystickytooltip1" class="stickytooltip">
<div id="mystickytooltip2" class="stickytooltip">
<div id="mystickytooltip3" class="stickytooltip">

And I need the javascript to read "mystickytooltip(ANYVALUE)" is this possible? My JS knowledge sucks.

//stickytooltip.init("targetElementSelector", "tooltipcontainer")
stickytooltip.init("*[data-tooltip]", "mystickytooltip")

Thanks.

A: 

You can try this :

var i=1; // starting index
while(document.getElementById('mystickytooltip'+i)) {
  stickytooltip.init("*[data-tooltip]", "mystickytooltip"+(i++));
}

In english : as long as you can find an element with id 'mysticktooltip'+i (+ is string concatenation in js), call stickytooltip.init with this id as second parameter (and increment i).

Alsciende
Sorry, it didn't work. The page is at www.euroworker.no/order (Add items to basket (more than one) with Kjøp and Handlevogn)
Kyle Sevenoaks
What went wrong?
Alsciende
Canæt say, it just didn't work. I'm using an example from http://www.dynamicdrive.com/dynamicindex5/stickytooltip.htm
Kyle Sevenoaks
+1  A: 

In your previous (now deleted) question, it looked like you were using jQuery. If that's the case, you should just be able to use a class selector:

var myToolTips = $("div.mystickytooltip");

Or you could use the Attribute Starts With selector:

var myToolTips = $("div[id^='mystickytooltip']");

The only possible drawback I could forsee with the attr-starts-with selector is if you have any other div elements whose id begins with "mystickytooltip" (ie, "mystickytooltipcontainer"). In that case you could combine the class and attr-starts-with selectors:

var myToolTips = $("div.mystickytooltip[id^='mystickytooltip']");
Andy E
This looks like it could be the one, but just a noob js question, I'd just add this in place of where "stickytooltip.init("*[data-tooltip]", "mystickytooltip")" is yes?
Kyle Sevenoaks
@Kyle Sevenoaks: It looks like you've not followed the instructions correctly for the sticky tooltips script you linked to in Alsciende's comments. After a brief read of that page it appears you have the structure of the tooltips confused.
Andy E
I am not surprised haha. Told yo umy JS knowledge is like, literally none. I built some array once a couple of years ago. This is way out of my league. I shall see ifI can get an example working in jsbin.com
Kyle Sevenoaks
http://jsbin.com/aquka/edit here, this works as per the example, just need to add it to my smarty foreach loop..
Kyle Sevenoaks
I've abandoned this solution and gone with a much simpler CSS only tooltip. Thanks for trying to help!
Kyle Sevenoaks
Answer accepted as most useful to figuing out I misunderstood the structures.
Kyle Sevenoaks
Hey @Kyle: sorry, I was out for a meeting, glad you found a good solution anyway.
Andy E