Skip to content

Instantly share code, notes, and snippets.

@jpetto
Created August 31, 2012 00:39
Show Gist options
  • Save jpetto/3546641 to your computer and use it in GitHub Desktop.
Save jpetto/3546641 to your computer and use it in GitHub Desktop.
DOM Demo - Remove elements & change text
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo - Remove DOM Elements &amp; Replace Text</title>
</head>
<body>
<section id="main">
<header>
<h1>Space Ghost</h1>
<h2>Coast to Coast</h2>
</header>
<aside id="show-notes">
<h3>Show Information</h3>
<dl>
<dt>Title</dt>
<dd id="title">Knifin' Around</dd>
<dt>Original Air Date</dt>
<dd id="air-date">September 2, 2001</dd>
<dt>Guests</dt>
<dd id="guests">Bjork, Thom Yorke</dd>
</dl>
</aside>
<h2>Quotes</h2>
<blockquote id="quote1">
Now you listen to me. I could go to jail in Mexico if Thom were to hear that I'm copying his CD.
Don't look at me! We're talkin' about dragons. So you take Thom out to the set while I burn and
verify these...these...dragons.
</blockquote>
<blockquote id="quote2">
Look, just tell this woman that she's crazy. Just because I'm famous and sexy doesn't mean that
someone can just go and marry me the second I leave the room.
</blockquote>
<blockquote id="quote3">
What, are you kidding, they're my best friends! That's why I married you, so I wouldn't have
them anymore.
</blockquote>
</section>
<button id="remove-quote" type="button">Remove Quote #3</button>
<button id="change-date">Change Air Date</button>
<script type="text/javascript">
var btn_quote = document.querySelector("#remove-quote");
var btn_date = document.querySelector("#change-date");
btn_date.addEventListener('click', function() {
var date = document.querySelector("#air-date");
date.innerHTML = '09/02/2001';
});
var remove_quote = function(quote_number) {
var quote = document.querySelector("#quote" + quote_number);
if (quote.parentNode) {
quote.parentNode.removeChild(quote);
}
};
btn_quote.addEventListener('click', function() {
remove_quote(3);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment