/* Just basic CSS to make the button prettier - nothing too fancy */
.btn {
padding: 15px;
background-color: #000;
color: #fff;
}
JavaScript
// Creating a function to run when called
function displayMessage() {
// This is to allow the function to work in the site's HTML
var html = document.querySelector('html');
// This creates a "button" element within the "html" section
var panel = document.createElement('button');
// This means that all the additional things we update will happen within the "panel" section
html.appendChild(panel);
// This set's the button's class to "btn"
panel.setAttribute('class', 'btn');
// This adds another attribute called "data-text-swap" to the button, and sets the text to "Schleifer"
panel.setAttribute('data-text-swap', 'Schleifer')
// This sets the text inside the button to "Danielle"
panel.textContent = "Danielle";
}
//This runs the function above.
displayMessage();
// This provides the functionality to actually make the button work
// This selects everything within the document that has the ".btn" class
var button = document.querySelectorAll(".btn")[0];
// This provides the logic for anytime anyone "clicks" the button using a basic if/else statement.
button.addEventListener('click', function() {
if (button.getAttribute("data-text-swap") == button.innerHTML) {
button.innerHTML = button.getAttribute("data-text-original");
} else {
button.setAttribute("data-text-original", button.innerHTML);
button.innerHTML = button.getAttribute("data-text-swap");
}
}, false);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.