0

I want to write a new version of my jQuery extension but I want to ask before diving into it. So here is what I have and what I want, I am not sure whether I am goind in the good way with this question so feel free to express your opinions. =)

Dynamic element creatin with jQuery - I have always been kinda 'lazy' with

$(selector).html("<h2>I rule!</h2>");

so I wanted something else and here I go:

var div = $("body > .wrapper > div.my-class[data-help=custom help message]").mk();

So actually what this extension does is that it creates a 'div' element with attributes of class and data-help and appends it to 'body > .wrapper' (the last '>' is the indicator where to apend it) immediately, and finally returns the 'div'.

So as you can see, this is more like a 'descriptor' than a selector. Fine, but...

What I want to achieve is this: When I am calling $(), I want to write a code which makes a decision first before letting jQuery does its job. So this is what I thought of it could look like:

//this doesn't append to any element,
//only returns a new 'div' element
var div = $("make:div.my-class[data-help=custom help message]");

As you can see I would like to 'prepocess' the selector before it gets digested by jQuery. I want to check whether the selector contains

if ((/^make\:/i).test(selector)) { ... }

and if it does, I want to call my extension with selector.subsrting(5).
So basically I want to push a command inside the selector and execute my extension before letting jQuery do its job.

So is there any way achieving the desired result?

Anyways, it is a nice experiment for me... =)

Thanks in advance! =)

2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by benqus
0

Yea, that's cool, thank you!
However I am still looking for a solution which enabled selectors to work as commands. For example:

//I want this to create a div, with a class of 'my-class'
//I have the class - and other attributes - creation part
$("make:div.my-class");

//instead of this - this just doesn't sound so good... =)
$("div.my-class:make");

I already have the creation part which enables "descriptors", I oly need the "make:" part... =)

0

I think you can somehow pre-read and process the selector in the

$.fn.init

method... I just don't know how...
I mean I have tried, it's just screwes up the whole jQuery... =))))

My code looked something like this:

(function ($) {
    var init = $.fn.init;

    $.fn.init = function ($) {
        //how to get the freakin' selector????
        //is it $.selector???
        if ((/^make\:/i).test($.selector)) {

            //cutting off 'make:'
            var sel = $.selector.substring(5);

            init;

            //here comes my plug-in
            $(sel).mk();
        }
    }
})(window.$);

This is all I want, I just don't know how to get this sh!t working... =))

Edited by benqus: missing '$' as parameter from new function

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.