0

I am writing a script that takes a book as some array of sentences. I want to print each sentence out, then make each sentence clickable. Right now, I have it printing each sentence, but when I attempt to append a

element with it's own unique id for each sentence, it does not work. here is my jsfidde: http://jsfiddle.net/mvvq8L7p/

Relevant code snippet.

var IdCounter = 0;
for (var i = 0, len = book_sentences.length; i < len; i++) {
   IdCounter++;
   document.getElementById("pageOfBook").innerHTML = "<p class='sentence' id='sentence#" + IdCounter + "'>" + book_sentences[i] + "</p>";
}

Can anyone explain why this occurs and how to make it work? Thanks in advance, I really appreciate the help!

4
  • why are you looping through complete paragraph ? why don't you just split it with '.' ? Commented Feb 24, 2015 at 6:14
  • What do you mean? I am not familiar with split... Commented Feb 24, 2015 at 6:19
  • See my answer, and let me know if you still find any issue, or mark is as answer if you find it helpful. Commented Feb 24, 2015 at 6:26
  • Works great thanks a lot! A lot more simple than my way haha Commented Feb 24, 2015 at 16:23

1 Answer 1

2

Simply just split your paragraph with '.', and append it to your div with IDCounter/

    var book = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
var book_sentences = book.split('.');
IdCounter = 0;

book_sentences.forEach(function(data){
IdCounter = IdCounter + 1;
    console.log(data);
document.getElementById("pageOfBook").innerHTML += "<p class='sentence' id='sentence#" + IdCounter + "'>" + data + ".</p>";
});
<div id="pageOfBook"></div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.