Devilryo
Devilryo

Reputation: 33

Jquery : how to add attributs dynamically with the function .attr()

I need to add my attributes dynamically to my input but it dosen't works.

Here is my code :

Component("Input", {
    add: function(element, type) {
       html = $('<'+this.nomRoot+' type="'+ type +'" />').appendTo('#'+element);

       for(key in this.lstAttr) {
          alert(this.lstAttr[key]);
          alert(this.lstAttrValue[key]);
          $(html).attr("'"+this.lstAttr[key]+"', '"+this.lstAttrValue[key]+"'");
       }

    }
});

So my code run with firebug but dosen't add the attributs.

I tried something like that :

Upvotes: 3

Views: 133

Answers (1)

epascarello
epascarello

Reputation: 207557

You want to set the attribute and the value, just reference the two indexes. No need to build some sort of string.

$(html).attr(this.lstAttr[key], this.lstAttrValue[key]);

Upvotes: 3

Related Questions