Page 1 of 1

[HTML/JavaScript] Creating a div by runtime

Posted: Thu Apr 03, 2014 5:38 pm
by es_91
THIS POST HAS BEEN SOLVED. PLEASE SEE SECOND POST FOR THE ACTUAL PROBLEM!

Propabbly, there are a lot of java gurus around here, so i'll try this here:

Code: Select all

<!DOCTYPE html>
<html><head><script type="text/javascript">function killElement(element) {If (element) {var papa = element.parentNode;If (papa) papa.removeChild(element);}}</script>
</head>
<body>
<font face="Courier"><div id="background" style="width:720px;">
</div></font>
<script type="text/javascript">
document.getElementById("background").innerHTML = "Guten Tag.<br />"
</script>
<script type="text/javascript">
killElement(document.getElementById("background"));
</script>
</body>
</html>
Why is not the <div> part of the web site deleted?

/EDIT: Solved. Thanks to RSBasic!

Code: Select all

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">
             onload = function() {
                var papa = document.getElementById("background");
                if (papa) {
                    papa.innerHTML = "Guten Tag.<br />"
                    papa.parentNode.removeChild(papa);
                }
            }
         </script>
    </head>
    <body>
        <font face="Courier">
            <div id="background" style="width:720px;"></div>
        </font>
    </body>
</html>

Re: [HTML/JavaScript] Pleeease delete this <div>! [SOLVED]

Posted: Thu Apr 03, 2014 11:07 pm
by es_91
another question ... why can't i create a new <div> element as a child to a span element?

Code: Select all

<!DOCTYPE html>
<html>
<head>
	<script type="text/javascript">
		onload = function() {
			var neuerBackground = document.createElement("div");
			neuerBackground.style.color = "#FF0000";
			document.getElementById("main").appendChild(neuerBackground);
		}
	</script>

</head>
<body>
	<span id="main"></span>
</body>
</html>


Re: [HTML/JavaScript] Creating a div by runtime

Posted: Fri Apr 04, 2014 6:26 am
by helpy
Check the html specification and the differences between flow content and phrasing content.

Re: [HTML/JavaScript] Creating a div by runtime

Posted: Fri Apr 04, 2014 3:37 pm
by es_91
Thanks. :)