0

Hi at the moment i have some Javascript that makes my image loop through automatically multiple images stored in a array, but how can i do the same thing with text on a H2 text object?

<SCRIPT LANGUAGE = "Javascript">
    var p1 = new Image();
    var p2 = new Image();
    var p3 = new Image();
    var p4 = new Image();

    p1.src="images/223.png";
    p2.src="images/335.png";
    p3.src="images/225.png";
    p4.src="images/226.png";

    var imgArray = new Array (p1, p2, p3, p4);

    var counter = 0;
    var loop = 1;
    setTimeout(scroll_forwards, 2000);

    var end = imgArray.length - 1;

    function scroll_forwards() {
        if (counter == end){
            counter = 0;    
        }
        else {
            counter++;
        }
        document.pic1.src = imgArray[counter].src;
        setTimeout(scroll_forwards, 2000);
    }
</SCRIPT>
2
  • Not clear..Post your html code or fiddle it. Commented Oct 13, 2013 at 5:38
  • you did it with the image, it should be much easier with the h1. Commented Oct 13, 2013 at 5:41

2 Answers 2

1

You just have to do the exact same thing you did with your image loop. I updated your code:

<script> // don't use the language attribute

    var imgArray = ["images/223.png","images/335.png","images/225.png","images/226.png"];
    var textArray= ["text1","text2","text3","text4"];
    var counter = 0;
    var h2Tag = document.getElementById("h2TagId"); // change this 
    var imgTag = document.pic1;
    var end = imgArray.length - 1;

    function scroll_forwards() {
        if (counter == end){
            counter = 0;    
        }
        else {
            counter++;
        }
        imgTag.src = imgArray[counter];
        h2Tag.innerHTML = textArray[counter];
        setTimeout(scroll_forwards, 2000);
    }
    setTimeout(scroll_forwards, 2000);
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

I don't know why it wasn't working when i attempted it last night but i just re-did the Javascript for the text and now it is working.... Thanks anyways and here is the code so anyone else knows.

<h2 id="imgh2">Change this</h2>



<SCRIPT LANGUAGE = "Javascript">
    var p1 = new Image();
    var p2 = new Image();
    var p3 = new Image();
    var p4 = new Image();

    var t1;
    var t2;
    var t3;
    var t4;

    p1.src="images/1232.png";
    p2.src="images/12312.png";
    p3.src="images/1241t.png";
    p4.src="images/12455.png";

    t1 = "Test 1";
    t2 = "Test 2";
    t3 = "Test 3";
    t4 = "Test 4";

    var imgArray = new Array (p1, p2, p3, p4);
    var textArray = new Array (t1, t2, t3, t4);

    var counter = 0;
    var loop = 1;
    setTimeout(scroll_forwards, 2000);

    var end = imgArray.length - 1;

    function scroll_forwards() {
        if (counter == end){
            counter = 0;    
        }
        else {
            counter++;
        }
        document.pic1.src = imgArray[counter].src;
        document.getElementById("imgh2").innerHTML = textArray[counter];
        setTimeout(scroll_forwards, 2000);
    }
</SCRIPT>

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.