146
rated 0 times
[
148]
[
2]
/ answers: 1 / hits: 35298
/ 10 Years ago, mon, september 15, 2014, 12:00:00
In jQuery, I can add multiple attributes to an element like so...
var input = $('<input/>').attr({ type : 'text', value : 'New Value'});
My question is, how can I achieve this using a variable like this...
var input = $('<input/>').attr(inputAttr);
I was under the assumption that inputAttr
should be an object and that I could add to that object. I must be mistaken. This was one of my many attempts to make this happen.
var inputAttr = {};
inputAttr.add({ type: 'text' });
inputAttr.add({ value : 'New Value' });
I also tried like this....
var inputAttr = {};
inputAttr.add('type: text');
inputAttr.add('value : New Value');
I thought maybe inputAttr
should be an array instead, which seems to output a correct string but not sure how to make it an object (which I think it should be).
var inputAttr = [];
inputAttr.push('type: text');
inputAttr.push('value : New Value');
// Then added object brackets like so
var input = $('<input/>').attr({inputAttr});
Any help would be appreciated!
Thank you in advance!
More From » jquery