jQuery syntax: var $div = $("<div>", test) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

jQuery syntax: var $div = $("<div>", test)

I have found the following code in a jQuery exercise: var test = { id: "div", class: "divclass", css: { "color": "Green" } }; var $div = $("<div>", test); I can’t understand the syntax in the last line. Does test add properties to jQuery object of $("<div>”)? I have never seen that two things are inside the brackets divided by a comma in a jQuery object: $(“a”, b)

6th Jan 2021, 11:57 AM
Prof. Dr. Zoltán Vass
1 Answer
0
Yes. Using this syntax we can add a new tag in our DOM. And here, set the properties of the div tag using the test variable. Ex:- var $div = $("<div>", {id: "foo", "class": "a"}); $div.click(function(){ /* ... */ }); $("#box").append($div); Hope you get it.
13th Jan 2021, 4:15 AM
Shaili Shah
Shaili Shah - avatar