Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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".

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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!)

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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 ...

  • #5
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    But ... there are built-in functions for that in jQuery

    Code:
    $('<div>').attr({
      id: 'mynewdiv'
    }).css({
      'background-color' : '#dddddd',
      'position': 'absolute'
    }).appendTo(document.body);

  • #7
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Sorry, you lost me completely


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •