views:

147

answers:

2

Hi, I wonder if anyone can help with a jQuery problem I am having.

I am using the tooltips from the Jquery Tools library to create a popup window when mousing over an hrefed image, this I want to use to cusomise the call to change the content in the DIV.

The links I am using are in the form:

<a href="/venue/1313.htm" class="quickView"><img src="/images/site/quickView83.png" alt="Quick View" width="83" height="20" /></a>

The code I am using to trigger the tip is:

$(".quickView").live('mouseover', function()
    {
        if (!$(this).data('init'))
        {
            $(this).data('init', true);
            ajax_quickView(); 
            $(this).tooltip
            ({ 
                /* tooltip configuration goes here */ 
                tip: "#quickViewWindow",
                position: "right",
                offset: [0, -300], 
                effect: 'slide' 
            });
            $(this).trigger('mouseover'); 
        }  
    });

I have tried the following function to grab the ID (in the example above, 1313) from the link:

function ajax_quickView(){
        var pageNum = $("a.quickView").attr("href").match(/venue/([0-9]+)/).htm[1];
        $("#quickViewWindow").load("/quick-view/", function(){}) 
    }

However I think this is where it falls down, I think my regex is prob to blame...

Once I get the var pageNum I presume I can just pass it into the .load as:

$("#quickViewWindow").load("/quick-view/", {id : pageNum }, function(){})

Many thanks

A: 

I don't know the tooltip plugin but I think your approach is a little bit... cumbersome. I worked with the jQuery tools and their overlay and it allowed to register a callback that would be handed the event source, in this case that would be the hrefd image which you can just ask for the actual URL. Might not be the answer you are looking for, but I just feel that you are making this more complicated than necessary.

ilikeorangutans
I did look at overlay, I actually use it somewhere else in my site. However it seemed to be a bit cumbersome in it's look and feel and forgive me if wrong, I was having trouble positioning it in relation to the link
bateman_ap
Oh, my code is prob terrible, I can't say I'm a whizz at all this lark, more teach myself by doing and asking!
bateman_ap
+5  A: 

Firstly, you haven't correctly escaped the / character in your regex:

/venue/([0-9]+)/

// should be
/venue\/([0-9]+)/

Secondly, you haven't correctly ended your regex, the entire line has a few syntax errors:

.match(/venue/([0-9]+)/).htm[1];

// should be
.match(/venue\/([0-9]+).htm/)[1]; 
JJ
Thanks, have changed that. However it only appears to be selecting the first link on the page, regardless of the one I have mouseover. Do I need to add anything to use the mouseover link?
bateman_ap
@bateman_ap: That's because you're using a selector in the jQuery function, which will return a list of elements and fetch the `href` attribute for the first in that list. In your `mouseover` handler you should pass a reference to `this`, e.g. `ajax_quickView(this)`, and use that reference instead of a selector.
JJ
Sorry, one last question. I used ajax_quickView(this); in my code, if I use alert(this); then it all looks good, it shows the correct URL. However in my function I am using function ajax_quickView(pageUrl){ var pageNum = pageUrl.match(/venue\/([0-9]+).htm/)[1]; $("#quickViewWindow").load("/quick-view/", {uid : pageNum}, function(){}) }Presume this is wrong as am getting: pageUrl.match is not a function
bateman_ap
`pageUrl` should be a DOM element, you should use `$(pageUrl).attr("href").match(...)`. This puts a jQuery wrapper around the element, which will expose the `attr` method to retrieve the href string which you can call `match` on.
JJ
And thats why I love Stack Overflow! Many thanks for all your help, all perfect.
bateman_ap