I’m writing a javascript library and I need to use jquery’s append method to add content to a div. I thought that append() treats "straight strings" as text and $("<div>html objects</div>") as html but apparently if you use append("<p>") then you will get the string parsed into html which means that strings need to be encoded before being appended.
I would like to use something which works like append but which makes distinctions between strings which are to be treated literally and strings which are to be parsed BASED ON THE PARAMETER. Since this is a library I wouldn’t like to duplicate all my functions to make text() and html() versions of each.
For example, I want to be able to add the string "<p>xx</p>" without it being parsed to html but when I want to add the same string as html then I would pass $("<p>xx</p>") as a parameter instead.
edit: Basically I’m tired of seeing tutorials use append() to append text, because it does not append text, it appends html. I want to know if there is a method which distinguishes appending strings from appending html objects.
1 Answer