DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Input And Textarea Preview

  • submit to reddit
        Preview what you type in an input or a textarea out of them (in a html tag, like a p, hx...)

Seen here: (spanish explain).

javascript code:

function preview(id1, id2){
	  		var NewText = document.getElementById(id1).value;
	  		splitText = NewText.split(/\n/).join("<br />");
	  		var DivElement = document.getElementById(id2);
	  		DivElement.innerHTML = splitText;

html code:

<form action="#">
<input type="text" id="nombre" onkeyup="preview('name', 'preview-name');" />

<input type="text" id="phone" onkeyup="preview('phone', 'preview-phone');" />

	<dd id="preview-name"></dd>
	<dd id="preview-phone"></dd>