tags:

views:

19

answers:

1

I am trying to extend DOMWindow example 6 to anchor to various elements at the page. As it is, the syntax implies that selector needs to be static, be it class name or id, both work. What I'd like to do, however, is assign the id of the clicked element as a selector, but it doesn't seem to work. A simple example:

$(function() {
        $('.example6DOMWindow').openDOMWindow({
            height: 100,
            width: 300,
            positionType: 'anchored',
            anchoredClassName: 'exampleWindow6',
            anchoredSelector: '#someid',
            eventType: 'click',
            windowSource: 'ajax',
            windowHTTPType: 'post'
        });
    });

I tried to specify selector as anchoredSelector: '#' + $(this).attr('id'), but .openDOMWindow cannot be used in the same way as .click(), so $(this) does not actually refer to the clicked element. I also tried the following, but it didn't work either:

$(function() {
        $('.example6DOMWindow').click(function() {
           var link = $(this);
           link.openDOMWindow({
               height: 100,
               width: 300,
               positionType: 'anchored',
               anchoredClassName: 'exampleWindow6',
               anchoredSelector: '#' + link.attr('id'),
               eventType: 'click',
               windowSource: 'ajax',
               windowHTTPType: 'post'
           });
         });
    });

Does anyone have suggestions for how this could be achieved? Or maybe alternative plug-ins that can achieve the same functionality?

+1  A: 

You can do this generically for pretty much any plugin that needs an option based on the element you're on with a .each() loop, like this:

$(function() {
  $('.example6DOMWindow').each(function() {
    $(this).openDOMWindow({
        height: 100,
        width: 300,
        positionType: 'anchored',
        anchoredClassName: 'exampleWindow6',
        anchoredSelector: '#' + this.id,
        eventType: 'click',
        windowSource: 'ajax',
        windowHTTPType: 'post'
    });
  });
});
Nick Craver
Works perfectly, thanks. Good to know for the future too. I started working along the lines of $('.example6DOMWindow').click(function() { $.openDOMWindow({..});}); but it wasn't quite working out, the link was following through for some reason.
Shagglez