...

View Full Version : appendChild Line Break Question



s0cb
02-24-2009, 07:55 PM
This question is quite rudimentary hence I am a newbie to JavaScript.
How may I perform a line break / new paragraph break so it may show up as

-BUTTON-

Hello ASDF

Hello ASDF

Hello ASDF

etc...





<head>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
function button()
{

var txt = ("Hello ASDF");
var newtext = document.createTextNode(txt);
document.body.appendChild(newtext);

}


</SCRIPT>
</head>

<body>


<a id="myAnchor"></a>
<INPUT TYPE = "button" NAME = "button()" VALUE = "Hi" onclick = "button()"/>

itsallkizza
02-24-2009, 08:04 PM
You can add <p> elements to the page and style them accordingly in CSS:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example</title>
<style type="text/css">
p
{
margin: 0;
padding: 4px 0 0 0;
}
</style>
<script type="text/javascript">
// <![CDATA[

function addElement(to,element_type,inner_html)
{
var new_elm = document.createElement(element_type);
if (new_elm)
{
new_elm.innerHTML = inner_html;
to.appendChild(new_elm);
return new_elm;
}
return null;
}

function addPToBody()
{
addElement(document.getElementsByTagName("body")[0],"p","new line of text");
}

// ]]>
</script>
</head>
<body>

<input type="button" value="add paragraph" onclick="addPToBody()" />

</body>
</html>


You can try out different styles:


p
{
/*default browser p style*/
}

p
{
margin: 0;
padding: 0;
}

p
{
margin: 0;
padding: 0;
display: inline;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum