2

I would like to display one of arrays after clicking matching array: for example

  • image 1 after clicking will display array 1
  • image 2 after click will display array 2:

var DetailArray = new Array(
            "number 1",
            "number 2",
            "number 3"
        );

function ShowNumber(number) {
 document.getElementById("number").value=  ;
}

img src image 1 calling function: ShowNumber();

and paragraph when it would be displayed

<p id="number"></p>

but I don't really know how to make a loop which will take from arrays matching string.

3
  • arrays are numerically indexed, just correlate the image with the index, and then it's a simple matter from there. Commented May 1, 2015 at 17:07
  • 1
    .value is for input elements. You can't use it with <p>. Commented May 1, 2015 at 17:07
  • Pro tip: define your array using [] instead of new Array, which can have unintended side effects in some situations. Commented May 1, 2015 at 17:11

1 Answer 1

3

Use textContent or innerHTML to set the contents of a paragraph or DIV, value is for the value of user inputs.

function ShowNumber(number) {
    document.getElementById("number").textContent = DetailArray[number];
}

Image 1 can then contain:

onclick="ShowNumber(0)"

to show number 1.

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.