Is there a jquery/javascript function that does the same that PHP echo()?
7 Answers
I suppose the closest is document.write() as it's native javascript.
However there are many methods of writing/amending text in the DOM, such as innerHtml(), innerText() and outerHtml(), as well as jQuery's html() and text().
1 Comment
document.write native JS though - document is by definition using the DOM, even if what you then do is stream text to be written. Note also that innerHtml and outerHtml are not methods but properties on the regular DOM (even though the latter is a method in jQuery).If you want to edit the content of an element use html().
You can manipulate the DOM in diffrent ways. Check the docs.
Comments
I know there is such a simple way to go around it, and we can simply use document.write method. But the downside of that default method is that it simply appends the string passed to the method directly to the body.
In case we want specific tags to be appended we can use the function i made below. The function name is printThis() and takes three parameters text, className, idName. The text is the text which we want to append to the document, the className and idName are the name of the class and id we want to set.
function printThis (text,className,idName){
var p = document.createElement('p')
var t = document.createTextNode(text);
setAttribute = function (el, attrs) {
for(var key in attrs) {
el.setAttribute(key, attrs[key]);
}
}
setAttribute(p,{class:className,id:idName})
p.appendChild(t)
document.body.appendChild(p)
}
printThis('This is a new Paragraph','newClass','newId')
echois not a function (it's a language construct) and you're probably looking fordocument.write('Hello World');.