We are currently learning how to use the DOM and some javascript commands to make dynamic websites. My main and only problem so far is that my CSS won't apply to the HTML I have added to my <p>
tag with the id="paragraph"
:
1.What am i doing wrong?
2.What can i do differently?
3.What can help clean up my code?
function Music() {
var string = document.getElementById('text').value.match( /[^\.!\?]+
[\.!\?]+/g ); // This searches through our text, puts results into an
array
for (var i = 0; i < string.length; i++) { // This goes through our "string array"
var space = string[i].match(/\s/g ); // Counts the amount of
spaces in each string.
var wrapEl = document.createElement("span"); // Create a span tag
wrapEl.innerHTML = string[i]; // place strings into span tag
// This clears the paragraph element and refills it with the new text output.
if (string.length > 0) {
document.getElementById("paragraph").innerHTML = "You didn't
write anything!";
} else {
console.log("You Didn't write anything!");
}
switch (space.length) {
case space <=2: //Tiny (0-2 words) - Yellow Highlight
span.addClass("style1");
break;
case space <=5: //Short (3-5 words) - Red Highlight
span.addClass("style2");
break;
case space <=14: //Medium (6-14 words) - Green Highlight
wrapEl.addClass("style3");
break;
case space >15: //Long (15+ words) - Blue Highlight
wrapEl.addClass("style4");
break;}
var paragraph = document.getElementById("paragraph");
paragraph.appendChild(wrapEl); // append span tag to paragraph tag via id
}
}
!!!For reference, Im using a tag in my HTML as the place to type your text. And i put all my CSS classes in the with the tag.!!!