Reputation: 815
Hello I am looking for some working code to create span elements.
My text is simple each word is seperated by space.
I need to create span as follow so that I can lift the code quickly. As mention below each word has unique id as W1, W2, etc.
<p>
<!-- I need to create span as follow so that -->
<span id="W1">I</span>
<span id="W2">need</span>
<span id="W3">to/span>
<span id="W4">create</span>
<span id="W5">span</span>
<span id="W6">as</span>
<span id="W7">follow</span>
<span id="W8">so</span>
<span id="W9">that</span>
</p>
Thanks.
I have MS expression web and vb studio, any tool I can keep locally and keep creating.
I see this link on web doing something.
Upvotes: 0
Views: 5512
Reputation: 771
// You either have a string
var mysentence = "I need to do my homework";
// or a paragraph
<p id="words">I need to do my homework</p>
// get sentence
var mysentence = document.getElementById('words');
function spanify(sentence) {
var arrayOfStrings = sentence.split(" "), newString = "";
for (var i=0; i < arrayOfStrings.length; i++){
newString += "<span id='W" + i + "'>" + arrayOfStrings[i] + "</span>";
}
return newString;
}
// spanify the sentence
spanify(mysentence);
Upvotes: 1
Reputation: 3731
Html
<body>
<p>
</p>
</body>
Js
<script>
var sString = "asdf sadfasd f sdfasd fasdfasdfasdf";
var aString = sString.split(' ');
for (var i=0;i<aString.length;i++)
{
document.getElementsByTagName("p")[0].innerHTML += '<span class="w' + i + '">' + aString[i] + '</span>'
}
</script>
And always post your code, even if it seems to you foolish and wrong :)
Upvotes: 1