[jQuery] recursive dom walker

[jQuery] recursive dom walker


I am trying to write a plugin that will eventually match a specific
string pattern on the id attribute of all form element(s)
I aim to get a collection of all elements that I want to return for
chaining
I generate a regular expression on the fly based on the arguments
passed in and use this to find an initial set of elements
I have this returning jquery objects using this
$(":input").filter(function() {
//return this.id.match(regEx);
return $(this).attr("id").match(regEx);
})
this also works
$(":input").filter(function() {
return $(this).attr("id").match(regEx);
}).setDisabled(true);
But when I use this $("#a_1").recurse({source:"a_1"}).setDisabled
(true); ,my plugin function I get this
TypeError: $("#a_1").recurse is not a function
Its my first attempt at a plugin and I guess I am not returning the
jquery object and I am not sure if this is the correct approach
Anyway any advice would be much appreciated
Full code below
(function($) {
$.recurse = function(options) {
var defaults = {
delimeter: "_"
, level: 1
, dir: "down"
, source: "body"
}
var settings = $.extend({}, defaults, options);
// funtion to find regEx
function generateRegEx(delimeter, src, direction) {
console.log(src.split(delimeter));
console.log(src.length);
// need to have the concept of next and prev
// so for prev or back take away a wholePattern match
// and fro next or forward add a pattern match
// so based on src and the up or down param
var dir = direction
arr = src.split(delimeter);
var arrLength = arr.length;
console.log(arrLength);
if (arrLength <= 1 && direction == "up") {
//alert("the only way is forward");
throw Error("you can only go up from here");
direction = "up";
}
arrLength = (direction == "up") ? arrLength - 2 :
arrLength;
//^([0-9a-zA-Z-]+\_){3}$ - this gets all the pattern does
ntot take account of src
// supercedes the one above ^a_1(\_[0-9a-zA-Z-]+){2}$
var delPattern = src + "(\\" + delimeter + "[0-9a-zA-Z-]
+)"; //this will match the character that separates the levels
var numerOfPattern = "{" + arrLength + "}"; //this matches
all alpha-numeric characters in between the delimeters
var wholePattern = delPattern + numerOfPattern; // this
combines them both
console.log(wholePattern);
var pattern = "^"
pattern += wholePattern;
pattern += "$";
console.log(pattern);
return new RegExp(pattern);
};
var regEx = generateRegEx(settings.delimeter, settings.source,
settings.dir);
$(":input").filter(function() {
//return this.id.match(regEx);
return $(this).attr("id").match(regEx);
})
};
})(jQuery);