Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-18-2012, 10:29 AM   PM User | #1
ammuhere
New to the CF scene

 
Join Date: Feb 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
ammuhere is an unknown quantity at this point
jquery:-how to assign variable value to jquery?

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".
ammuhere is offline   Reply With Quote
Old 02-18-2012, 01:32 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Your question is far from being understandable ...

Please tell us what you want to do (not technically but rather logically)

Example technically (not good): I want to set a variable so that I can set an attribute

Example logically (better): How can I set an attribute of an element to a variable value? (if this is what you want ... I don't know!)
devnull69 is offline   Reply With Quote
Old 02-18-2012, 02:35 PM   PM User | #3
ammuhere
New to the CF scene

 
Join Date: Feb 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
ammuhere is an unknown quantity at this point
Please see the more detailed code.

Code:
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
}

}

} }
jQuery(elem).nodelist[node](attr,eval("elm"+attr)):-
so in the above line while executing this in the for loop the first time i can get the value of "attr" and second time "css".
Please let me know if any more clarification needed and help me how to resolve this.
ammuhere is offline   Reply With Quote
Old 02-18-2012, 03:02 PM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
So you came up with another technical explanation ... you did not say what you WANT to do, you just told us what you TRIED do do.

This
Code:
jQuery(elem).nodelist[node](attr,eval("elm"+attr))
does not make ANY sense to me ...

1. jQuery(elem) does not have a property nodelist (which is an object literal and not a method/property of a jQuery object)
2. You cannot call a function with parameters attr and eval("elm"+attr) on one of the entries of the nodelist ...
devnull69 is offline   Reply With Quote
Old 02-18-2012, 03:31 PM   PM User | #5
ammuhere
New to the CF scene

 
Join Date: Feb 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
ammuhere is an unknown quantity at this point
i want to create DOM elements dynamically using jQuery.

so when i called something like this..
Code:
var element_wrapper=$.create('div','divtest','','','yes','orange','center','');
it should create div tag with all the attributes and style which are passing to the extended jQuery function as parameters.

I am giving the whole function below which i hope will give you some idea on what i am trying to do.

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;
          };
so i want to call the jQuery Function .attr and .css dynamically, as per the value that passed to the variable nodelist[node]. For that i used
jQuery(elem).nodelist[node](attr,eval("elm"+attr)); where nodelist have two values attr and css.

in my code everything works fine except the part of nodelist[node].
ie: 1. jQuery(elem).attr(attr,eval("elm"+attr));
and 2. jQuery(elem).css(attr,eval("elm"+attr)); is working fine.

so i assigned the values to an object and accessed it while creating the attributes.
var nodelist={ "nodename":"css" ,"nodename":"attr"}
So my question is how to refer a variable to call a function in jQuery?

Last edited by ammuhere; 02-18-2012 at 03:46 PM..
ammuhere is offline   Reply With Quote
Old 02-18-2012, 04:52 PM   PM User | #6
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
But ... there are built-in functions for that in jQuery

Code:
$('<div>').attr({
  id: 'mynewdiv'
}).css({
  'background-color' : '#dddddd',
  'position': 'absolute'
}).appendTo(document.body);
devnull69 is offline   Reply With Quote
Old 02-19-2012, 02:10 AM   PM User | #7
ammuhere
New to the CF scene

 
Join Date: Feb 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
ammuhere is an unknown quantity at this point
I m trying to create a library where all the values required to construct the DOM elements will be passed to the function and the function return the elements by constructing it using jquery. Using ur code i need to add 1000 lines of code extra to construct a page.
ammuhere is offline   Reply With Quote
Old 02-19-2012, 08:26 AM   PM User | #8
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Sorry, you lost me completely
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:48 PM.


Advertisement
Log in to turn off these ads.