1

I am working on my own Lorem Ipsum generator, with the added bonus of generating the corresponding HTML formatting code in a box beside it.

So, the paragraph is generated via this button

<button id="generate" type="button" onclick="LoremIpsumRandom()">1 Paragraph</button>

and is generated here <p id="textarea"></p>

function LoremIpsumRandom()
{
    //global to store previous random int
     _oldInt = null;

var pickRandom = function(paragraphArray)
    {
        //random index of paragraphArray
        var randomInt = Math.floor(Math.random()*paragraphArray.length);
        //ensure random integer isn't the same as last
        if(randomInt == _oldInt)
            pickRandom(paragraphArray);
        else{
            _oldInt = randomInt;
            return paragraphArray[randomInt];
            }
    }
    //your lorem ipsum paragraphs
    var paragraphArray =     [
                            "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.", 
                            "Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.", 
                            "Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.", 
                            "Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur?", "Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?", 
                            "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.", 
                            "Et harum quidem rerum facilis est et expedita distinctio.", "Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.", 
                            "Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae.", 
                            "Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."
                            ];  

//update element content (e.g. `<div>` with paragraph
    document.getElementById("textarea").innerHTML = pickRandom(paragraphArray);
    //document.getElementById("textarea-code").innerText = pickRandom("&lt;P&gt;" + (paragraphArray) + "&lt;&#47;P&gt;");



}

No point including the CSS to be honest - it's just standard text boxes and auto-generated buttons.

So, the issue is that although I can generate the normal paragraph in the id="textarea" section, I want to be able to generate a raw HTML version as well (with the paragraph and line breaks code being shown.) So far, I've been able to generate the raw HTML with no issues, but when I added @pixelbobby 's section (which makes sure that when a sentence is selected, the next selection won't be the same, as true randomness can sometimes generate), the code doesn't work. In some instances, it's generated the raw HTML, but the generated paragraph is a different one.

The other functions (generating lists, multi-paragraphed sections and multi-levelled lists) utilise the raw HTML and output correctly, and it all runs smoothly. The raw HTML is always outputted to <p id="textarea-code"></p> which is next to the first textarea section.

Can someone tell me how I can modify the script so that the raw HTML part of the script generates the same paragraph as the result of the pickRandom(paragraphArray)

So, This - document.getElementById("textarea-code").innerText = pickRandom("&lt;P&gt;" + (paragraphArray) + "&lt;&#47;P&gt;"); needs to be the same as - document.getElementById("textarea").innerHTML = pickRandom(paragraphArray); but with the raw HTML specified in the pickRandom() part of the line.

Can anyone help?

Thanks :)

2
  • 1
    What has @pixelbobby told you? Link to it, please. Commented Jun 19, 2012 at 18:05
  • Earlier question: Select random paragraph from list Commented Jun 19, 2012 at 18:06

1 Answer 1

2

You can save the result of pickRandom(paragraphArray) in a variable and use it twice:

var randomParagraph = pickRandom(paragraphArray); //save the result of pickRandom

document.getElementById("textarea").innerHTML = randomParagraph;
document.getElementById("textarea-code").innerText = "&lt;P&gt;" + randomParagraph + "&lt;&#47;P&gt;");
Sign up to request clarification or add additional context in comments.

2 Comments

That's exactly what I was looking for! I guessed that I would perhaps need to combine it somewhere, but my lack of javascript knowledge (only started learning last night) meant I couldn't quite figure it out. So, thank you very much Zeta!
@tristanojbacon: If you just started JavaScript then the MDN JavaScript guide could help you.

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.