/* hw4.js */
/* Kristen Connal 2018 */
// Set up window.onload function
window.onload = function() {
//Check for button click and run function when clicked
var pgBtn = document.getElementById('divideTranscript');
pgBtn.addEventListener('click', changeText);
// Create function for button click
function changeText() {
// Pull div content
var div = document.getElementById('transcriptText');
var divText = div.textContent;
// Divide the text into an array of words
var divArray = divText.split(" ");
divArray = divArray.filter(function(str) {
return /\S/.test(str);
});
//Clear inner html of div
div.innerHTML = "";
// Iterate over array
for (i = 0; i < divArray.length; i++) {
// Build SPAN elements, along with attributes
var span = document.createElement('span');
var space = document.createTextNode(' ');
span.textContent = divArray[i];
space.textContent = " ";
span.setAttribute("className", "word");
span.setAttribute("id", "word" + i);
// Add SPAN elements to the original DIV
div.appendChild(span);
div.appendChild(space);
// Add event listener for mouseover - change background to yellow
span.addEventListener('mouseover', function() {
this.setAttribute("style", "background-color: #FFFF00;");
}, false);
//Add event listener for mouseout - change background back to white
span.addEventListener('mouseout', function() {
this.setAttribute("style", "background-color: #FFFFFF;");
}, false);
// Close the for loop, function, and window.onload
};
}
};
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.