I am creating a jQuery plugin that displays warnings on a page. The plugin itself inserts warning markup into the DOM. Since jQuery's way is to get everything back thisto maintain the chain, I came across an interesting problem that I would like to get feedback on. I am trying to solve the following two options.
Option one:
$("#content").alert('prepend', {type:'error', message:'This is an error'})
It looks pretty simple. A warning is added to the top of the #contentDOM element . The problem is that it is unclear what is returning. It would be wise to return the newly created alert element, but this method goes against jQuery.
Option Two:
$("<div>").alert({type:'error', message:'This is an error'}).prependTo("#content")
This method seems less clear and less intuitive, but it is more connected with the jQuery way of doing things, and it is clear which element will be returned.
So which options would you choose? . I am worried that most users may not know what you can do $('<div>')to create a new item. On the other hand, I do not know of any well-known projects whose jQuery plugin methods return elements that are different from the elements to which they refer, but possibly exist. Thoughts?
source
share