Menu
var s=”attr”
var i=$(s)
// jQuery(elem).attr(attr,eval(“elm”+attr));
jQuery(elem).$(s)(attr,eval(“elm”+attr));//i tried this.
how to assign a variable name in the above code(in place of s) so that i need to add an attribute to the element “elem”.
var elmlist={
"div": [
{
"div": "div"
},
{
"attr": [
"id": "id" ]
},
"css": [
{
"backgroundcolor": "background-color",
"textalign": "text-align"
}
]
},
{}
]
};
var nodelist={ "nodename":"css" ,"nodename":"attr"}
traverse(elmlist["div"]);
function traverse(propertylist){
for(var node in nodelist){
for (var attr in propertylist)
if(jQuery.type(propertylist[attr])=="string")
{
jQuery(elem).nodelist[node](attr,eval("elm"+attr)) ;//need modification here
//jQuery(elem).css("background-color","orange");//like this
}
}
} } [/QUOTE]
jQuery(elem).nodelist[node](attr,eval("elm"+attr)) :- in this from the nodelist[node] i can get the value of "attr" in the first time and second time "css". Please let me know if any more clarification needed and help me how to resolve this.
<i>
</i>jQuery(elem)[nodelist[node]]('attributename','attributeval);
<i>
</i>var nodelist = {
"nodename" : "attr",
"nodename" : "css"
}
<i>
</i>console.log(nodelist.nodename);
<i>
</i>var nodelist = ['attr','css'];
[CODE]var element_wrapper=$.create('div','divtest','','','yes','orange','center','');[/CODE]
[CODE]jQuery.create = function(elmtype,elmid,elmclass,elmchecked,elmstyle,elmbackgroundcolor,elmtextalign,elmtext) {
var args = arguments[0] || {}, elem = null;
var objname=elmlist[args]
if (args.constructor == String) {
if (arguments.length > 1) {
var typelist = objname[0];
for(var type in typelist)
{elem = document.createElement(type);
}
traverse(objname[1]);
function traverse(propertylist){
var nodelist={ "nodename":"css" ,"nodename":"attr"}
for(var node in nodelist){
for (var attr in propertylist)
if(jQuery.type(propertylist[attr])=="string")
{
jQuery(elem).nodelist[node](attr,eval("elm"+attr));
}
else{
// jQuery(elem).css("background-color","orange");
traverse(propertylist[attr]);
}
}
}
if ( !( jQuery.isEmptyObject(objname[2])))
{
for(var value in objname[2])
{elem = document.createTextNode(eval("elm"+value));
}
}
}
} return elem;
};[/CODE]
[CODE] var nodelist={ "nodename":"css" ,"nodename":"attr"}[/CODE]
<i>
</i>var myDOMElement = $('<div id="myid" style="background-color: #f00; text-align: center;"><em>A new dom element</em></div>');
<i>
</i>jQuery(elem)[methodname](argument1,argument2);
But as i heard using concatenated string in jquery have lot of disadvantages[/quote]
jQuery(elem)[methodname](argument1,argument2) ;[/QUOTE]
<i>
</i>//you can call the method using dot notation
jQuery(elem).attr('id','someID');
//you can use a quoted string
jQuery(elem)['attr']('id','someID');
//you can use a variable
var methodname = 'attr';
jQuery(elem)[methodname]('id','someID');
<i>
</i>$.fn.easyCreateDOM = function(tagname,cssprops,attributes) {
var htmlString = '<div style="';
for(i in cssprops) {
htmlString += i+':'+cssprops[i]+';';
}
htmlString += '" ';
for(i in attributes) {
htmlString += i+'="'+attributes[i]+'"';
}
htmlString += '/>';
return $(htmlString);
}
var CSSProps = {
'background-color' : '#f00',
'text-align': 'center',
'display' : 'inline'
};
var Attributes = {
'id' : 'someElemID',
'class' : 'a-class-name'
};
//myDIV is a jQuery object
var myDIV = $.fn.easyCreateDOM('div',CSSProps,Attributes);
console.log(myDIV);
var CSSProps = {
'background-color' : '#f00',
'text-align': 'center',
'display' : 'inline'
};
var Attributes = {
'id' : 'someElemID',
'class' : 'a-class-name'
};
[/QUOTE]
Figure out what your CSS properties and attributes are, create a string from it, and then wrap it in the jQuery function and it will return a DOM element:
[/QUOTE]
An alternative:
[code]
var CSSProps = {
'background-color' : '#f00',
'text-align': 'center',
'display' : 'inline'
};
var Attributes = {
'id' : 'someElemID',
'class' : 'a-class-name'
};
var myDIV = $('<div>').css(CSSProps).attr(Attributes);
I haven't tested it, but according to the docs, that ought to work.
0.1.9 — BETA 5.10