Course
What is JavaScript?-- operator-= operator++ operator+= operatorAccessing and setting contentArray concat() methodArray indexOf()Array lengthArray pop()Array shiftArraysBooleansBracesCallback functionCalling the functionClassClosureCode blockCommentConditionsConsoleConstructorCreating a p elementData typesDate getTime()DestructuringElseElse ifEnumEquals operatorError HandlingES6Event loopEventsExtendFetch APIFilterFor loopforEach()FunctionFunction bind()Function nameGreater thanHead elementHoistingIf statementincludes()Infinity propertyIteratorJSONLess thanLocal storageMapMethodsModuleNumbersObject.keys()Overriding methodsParametersPromisesRandomReduceRegular expressionsRemoving an elementReplaceScopeSession storageSortSpliceStringString concat()String indexOf()SubstringSwitch statementTemplate literalsTernary operatorTileType conversionWhile loop
Removing an element in JavaScript
To remove an element, we can use the removeChild
method on their parent. Here we need to remove the paragraph
element from the body
.
Start by coding bodyElement
, followed by the removeChild()
instruction.
jsx
<body id="parent">
<h2>Social Media</h2>
<p id="child">Meg: Movie later?</p>
<script src="script.js"></script>
</body>
jsx
document.getElementById("child").remove();
Then, we need to specify the child element we want to remove. Here, we add paragraph
between the parentheses to remove it from the bodyElement
.
jsx
<body id="parent">
<h2>Social Media</h2>
<p id="child">Meg: Movie later?</p>
<script src="script.js"></script>
</body>
jsx
const bodyElement = document.getElementById("parent");
const paragraph = document.getElementById("child");
bodyElement.removeChild(paragraph);