I am trying to make sense of the uses of the document.all and/or document.getElementbyId javascript functions. I just thought that I could use this code:
Code:
<script type="text/javascript">
function AppendNewLine()
{
document.write("In Function");
var myPar = document.getElementbyId("myP");
myPar+="\n";
myPar+="New Line?\n";
document.write(MyPar);
}
</script>
<p id="myP" value="value" >Append New Line<br /></p>
<input type="button" value="Append" onclick="AppendNewLine()" />
to append a new line to a paragraph on the page. Can someone tell me if I am on the right track, and where I went wrong, or am I all together wrong and where should I restart, and especially, WHY this doesn't work?
I've no idea what your code is supposed to be doing. document.getElementById, as the name suggests, returns an element, nothing else. You can use the innerHTML property of that element to change the contents.
Code:
function appendNewline () {
document.getElementbyId("myP").innerHTML += "New line\n (But if you want that to appear on the page) <br>";
}
You cannot use document.write after page load.
Also research DOM methods such as document.createElement, document.appendChild.
Great wit and madness are near allied, and fine a line their bounds divide.
a BR element is a non-empty tag (same as IMG, INPUT...). It can not have child nodes, so that you can append nothing to it. On the other hand: what's the use of appending a BR element inside a P element?
Post how do you expect your HTML code should look like. In fact, tell us what is your real need.
Basically, being very beginner, I bought a book (The Javascript Bible), and randomly found the beginning of Ch 15 on generic HTML Element Objects, and stumbled into all[] (and getElementbyId() since it's used in it's place in many situations) and their example:
Code:
var paragraph=document.all("myP");
simply got me curious to what I might be able to do with a <p> tag. If I can append text to different elements by id, then I can do it not just with <p> but <div>, for example, and have text appear pretty much anywhere on the page I want. My intentions are not for any particular project, just for learning.
function AppendNewLine(form)
{
var thep = document.getElementById("myP");
thep.appendChild(document.createElement("br"));
thep.appendChild(document.createTextNode("Is this on a new line?"));
}
Great wit and madness are near allied, and fine a line their bounds divide.
Bookmarks