Skip to content

Instantly share code, notes, and snippets.

@dotmh
Created November 25, 2011 14:22
Show Gist options
  • Save dotmh/1393625 to your computer and use it in GitHub Desktop.
Save dotmh/1393625 to your computer and use it in GitHub Desktop.
A jQuery Element Factory plugin (very simple)
/*
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)
(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)
@dotmh
Copy link
Author

dotmh commented Nov 26, 2011

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.

@dotmh
Copy link
Author

dotmh commented Nov 26, 2011

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