0

I have a simple array which has three elements. My elements are holding simple paragraph formatted text:

var quoteList = new Array(
"<p>One</p>", 
"<p>Two</p>", 
"<p>Three</p>"
);

This initialisation is done in my header. Now, in my body I print them by doing a simple document.write;

for(i=0; i<quoteList.length();i++)
{
    document.write(quoteList[i]);
}

This works, it prints out my three paragraph formatted text elements held in my array.

Now, I want to scroll through them when a, <a href> tag is clicked, but I'm having issues even getting some of my elements showing, and some hiding, see example;

quoteList[0].style.display="block";
quoteList[1].style.display="none";
quoteList[2].style.display="none"; 

This doesn't seem to be working. So, I have two issues:

  1. Cycling through my array elements on mouseClick
  2. I can't even get my quoteList elements hidden / visible.
2
  • 1
    jsfiddle.net/Sa7af Commented Mar 16, 2014 at 18:04
  • Awww thanks dfsg. I shall keep that as a reference :) Commented Mar 16, 2014 at 18:06

1 Answer 1

1

quoteList is an array of strings. After writing those strings to the document you create HTML elements and need to access them like that.

One solution is to add an id tag in the string phase: "<p id="p_0">One</p>" and then accessing it with document.getElementById("p_0").

Another option is creating the elements using document.createElement("p") and then appending them to the document. Because you've created the elements you can store them in an array.

A third option is retrieving all <P> tags using document.querySelector("p") and then iterating through them. Keep in mind that this will select all <P> elements in the document.

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

1 Comment

AHhh I can't believe I didn't think about adding a tag into my string phase. Should be able to work it all out now, cheers Gil

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.