Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  129] [ 1]  / answers: 1 / hits: 19957  / 11 Years ago, sat, january 4, 2014, 12:00:00

I have a paragraph that I'd like to delete the contents of.



document.getElementById(id).innerHTML = ;


doesn't seem to be working. Does anyone have a better solution?



Here's an example



<!DOCTYPE html>
<html>
<head>
<script>
document.getElementById(p).innerHTML = ;
</script>
</head>
<body>
<p id=p>
words
</p>
</body>
</html>


but the words in the paragraph are not removed. Thanks in advance to anyone that can help.


More From » html

 Answers
31
<!DOCTYPE html>
<html>
<head>
<!-- here the p tag doesn't exist yet -->
<script>
document.getElementById(p).innerHTML = ;
</script>
</head>
<body>
<p id=p>
words
</p>

<!-- however here it does exist -->
</body>
</html>


how to fix it ?



// only use this if you can't move your javascript at the bottom
window.onload = function() {
document.getElementById(p).innerHTML = ;
}


or move your javascript at the end of the page (this is the preferred one as javascript should always be loaded at the end of the page)



<!DOCTYPE html>
<html>
<head>
<!-- here the p tag doesn't exist yet -->

</head>
<body>
<p id=p>
words
</p>

<!-- however here it does exist -->
<script>
document.getElementById(p).innerHTML = ;
</script>
</body>
</html>

[#73371] Friday, January 3, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hasanjustusf

Total Points: 76
Total Questions: 116
Total Answers: 100

Location: Virgin Islands (U.S.)
Member since Tue, Jul 7, 2020
4 Years ago
;