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! =)

Recommended Answers

All 4 Replies

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... =)

Not sure jQuery provides an override for this. Only other option is to change the source.

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... =))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.