Created
November 25, 2011 14:22
-
-
Save dotmh/1393625 to your computer and use it in GitHub Desktop.
A jQuery Element Factory plugin (very simple)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
jQuery Simple Element Factory Plugin | |
==================================== | |
Version 1. By DotMH http://github.com/dotmh | |
This simply creates a html element , extended in jquery so all jquery's | |
functionality is there. | |
@param tag <string> : The tag you want to create , ie div (no < or > ) | |
@param options <object> : an object containing attribute name: value. | |
@returns <jQuery Element> : the new element | |
usage : | |
$.el('div' , {'id' : 'some_element' , 'class' : 'a_class b_class'}); | |
*/ | |
(function($){ | |
$.el = function(tag , options){ | |
var options_to_string = function() | |
{ | |
if ( options == undefined ) return ''; | |
var flat_options = []; | |
$.each(options , function(option , value){ | |
flat_options.push(option+'="'+value+'"'); | |
}); | |
return ' '+flat_options.join(' '); | |
} | |
return $('<'+tag+options_to_string()+'></'+tag+'>'); | |
}; | |
})(jQuery) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($){ | |
module('Core - Element Factory'); | |
test('Create Element' , function(){ | |
expect(4); | |
deepEqual($('<div></div>') , $('<div></div>') , 'Control'); | |
deepEqual($.el('div'), $('<div></div>'), 'Created Element With No Options'); | |
deepEqual($.el('div' , {id:'someId'}) , $('<div id="someId"></div>') , 'Created Element with an ID attribute'); | |
raises($.el() , 'No Parameters passed'); | |
}) | |
})(jQuery) |
Ok Fixed it , I need to use deepEqual not equal , I have updated the tests.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is my Element Factory and its Test file for QUnit , but it always fails even the control test. When I console.log them they appear to be the same.
Oh but when invoked the code works as expected.