tags:

views:

86

answers:

3

I have many problems when I create some web application that can dynamically load content. Because of script in loaded contain does not know where to be placed in document and how many duplicated object will be loaded in one document.

Therefore, I want to create some function for modified find function of jQuery object that will be used by script of loaded document like the following code.

<body>
     <div id="outer-div" class="interested-style">
     <div id="loaded-control-1">
          <div class="interested-style"></div>
     </div>
     <div id="loaded-control-2">
          <div class="interested-style"></div>
     </div>
     <div id="loaded-control-3">
          <div class="interested-style"></div>
     </div>
</body>

The script of "loaded-control-2" object could has some selector like the below code.

$('.interested-style')

In my example document, the above JavaScript code will return three objects. However, if I have some function for modified find function using the selector in specified area of code. The above script should return only one object that is correct result.

The solution might works like the following code.

var dynamic-script = loadSection('some-section-name');
var modified-jQuery = getModifiedjQuery('loaded-control-2');

(function()
{
    eval(dynamic-script);
})(modified-jQuery);

Thanks,

PS.1 If you directly modify jQuery object, another script on this page will use your new jQuery selector engine.

A: 

So, for this theoretical extension of the .find() function, how would it know where it was looking in the structure of the site to correctly find the specific element? Would this happen on a click event, or have any way of finding the element's parent? Could some sort of unique id be generated for each div with interested-style, giving you something unique to select by?

Frank DeRosa
From my design pattern, I always unwrite any selector in click or event function.
Soul_Master
+1  A: 

I'm not sure I understand what you want to do that cannot be done in jQuery today.

If you only want to modify the div with class interested-style that is a child of the div with class loaded-control-X then you can just use:

$('.loaded-control-X .interested-style')

This is know as the "ancestor decedent" selector, it will match any elements with a class interested-style that are descendants of any element with class loaded-control-X

If you only want the children of the element with class loaded-control-X you can use the "parent > child" selector:

$('.loaded-control-X > .interested-style')

The jQuery API is very flexible in how you can specify elements. See the jQuery Selectors docs for more.

Given your example if I want to apply a function to the div with class interested-style that is a child of the div with class loaded-control-2 I would use:

$('.loaded-control-2 .intereted-style').someFunction()
beggs
I know it can be selected by direct selector as you show.
Soul_Master
Not sure what the question is asking then, can you give more info/examples?
beggs
Please look at my recent question. http://stackoverflow.com/questions/1680030/how-to-solve-duplicate-objects-in-dynamic-loading-page-by-using-jquery
Soul_Master
A: 

I just see a optional parameter that is context can be use for limiting scope of selector.

jQuery( expression, [context] )

This function accepts a string containing a CSS selector which is then used to match a set of elements. The core functionality of jQuery centers around this function. Everything in jQuery is based upon this, or uses this in some way. The most basic use of this function is to pass in an expression (usually consisting of CSS), which then finds all matching elements. By default, if no context is specified, $() looks for DOM elements within the context of the current HTML document. If you do specify a context, such as a DOM element or jQuery object, the expression will be matched against the contents of that context.

For example, you can create some function like my recent question to automatic add parameter into every jQuery selector. Please look at the follwing code for clear understand.

// normal select
$('#some-object');

// You can limit scope of jQuery selector like this
$('#some-object', '#scope-object');

So, you don't need any modify to find function anymore.

Soul_Master