Click here to Skip to main content
16,004,564 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have to take the phrase
"It’s that time of year when you clean out your closets, dust off shelves, and spruce up your floors. Once you’ve taken care of the dust and dirt, what about some digital cleaning? Going through all your files and computers may seem like a daunting task, but we found ways to make the process fairly painless."

and
Divide the text into an array of words.
Iterate over that array, at each step:
Build SPAN elements as you go, along with the attributes as shown in the example
Add the SPAN elements to the original DIV
Add a click handler to the SPAN elements, or to the DIV, which causes the style on the SPAN to change on mouseover.

Im not necessarily asking someone to do it for me, I just need a point in the right direction as to where I should start
Posted
v2

  1. Divide the text into an array of words.


    Use JavaScript String split() Method[^]
  2. Iterate over that array, at each step:


    Use For Loop.
    JavaScript
    for (var i=0; i < myArray.length; i++)
    {
        // Do something.
    }


    • Build SPAN elements as you go, along with the attributes as shown in the example.


      JavaScript
      var yourSpan = document.createElement('span');
      yourSpan.innerHTML = "Hello";

    • Add the SPAN elements to the original DIV


      JavaScript
      var yourDiv = document.getElementById('divId');
      yourDiv.appendChild(yourSpan);

    • Add a click handler to the SPAN elements, or to the DIV, which causes the style on the SPAN to change on mouseover.


      JavaScript
      yourSpan.onmouseover = function () {
          alert("On MouseOver");
      
          // Do anything you want.
      }

 
Share this answer
 
v2
Comments
Member 10782197 29-Apr-14 12:01pm    
is there a way to build span elements without using innerHTML
So, how would you assign the text? What is the problem with innerHTML?
Member 10782197 29-Apr-14 14:49pm    
Actually nevermind that should be fine, but how do I use the split string function to split that whole phrase into an array
var str = "How are you doing today?";
var res = str.split(" ");

Please accept the answer, if it helped you. :)
Member 10782197 29-Apr-14 15:44pm    
so under
var str =" "
I would put the entire phrase?
and under str.split(" ") just putting a space will separate the words?

and I accepted it thank u
I'd suggest you start by dividing the text into an array of words. The String.split function should help you out there: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900