Irina Irina - 5 months ago 19
jQuery Question

What is this?! $(multiple parameters?)

I am trying to create some html elements with jQuery and i found this bit of code over the internet but i simply couldn't find any explanations or documentations on it (i've only just begun to learn jQuery) or i didn't knew how to search:

.after($('<div />', {
class: 'test',
text: "a div",
click: function(e){
e.preventDefault();
alert("test")
}}));


I know that .after() creates and adds an element after a specific html element, but i've only seen it used like this
.after("<tag>some text here</tag")
I have no idea were did that person get those key: value pairs. Are they "standard"? Does a list exist on the internet? What did he actually did there? How can i look for what's written between $() on the internet? I've tried something like "$() arguments jquery", ".after() multiple parameters jquery" and others but didn't find anything to make sense to me. I'd appreciate some light on this issue.
Thanks!

Answer

That's just after with an argument that's the result of calling $() with an HTML string to parse and additional properties to set; see jQuery(html, attributes) for details. It's basically like doing this:

var newElement = $('<div />');
newElement.addClass('test');
newElement.text("a div")
newElement.on("click", function(e){
    e.preventDefault();
    alert("test")
});
whateverWasHere.after(newElement);
Comments