2

Is there a jquery/javascript function that does the same that PHP echo()?

3
  • Define what "the same" is. Writing to a console? Writing into the current document? Displaying a message on screen? Commented Nov 17, 2011 at 13:58
  • 1
    First, echo is not a function (it's a language construct) and you're probably looking for document.write('Hello World');. Commented Nov 17, 2011 at 13:59
  • If you use echo to debug, maybe console.debug is what you may need. I suggest looking into firebug if this is what you were really looking for. Commented Nov 17, 2011 at 16:31

7 Answers 7

10

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().

Sign up to request clarification or add additional context in comments.

1 Comment

I wouldn't call 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).
3

You could use

document.write(stringToWrite);

or

document.writeln(stringToWrite).

Comments

2
// This is what you are looking for
document.write('Hello world');
document.getElementById('mydiv').write('Hello world');

// jQuery
$(body).append('Hello world');
$('#mydiv').append('Hello world');

Comments

1

I think you're looking for:

document.write( ... );

Comments

1
document.write("text"); \\javascript syntax.

while using jquery you can view you text under any element obj.

 $("#mydiv").append($("#myElement").val());

Comments

0

If you want to edit the content of an element use html(). You can manipulate the DOM in diffrent ways. Check the docs.

Comments

0

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.