javascript - change text and image on page load -


i added quote site , want quote change each time. works. want add picture changes text. (it has same picture same text).

how can this?

my code:

<script type=text/javascript>  var delay="10"; //how many seconds wnat delay var count='0'; var texts=new array(); texts[0]="je voelt je hier geen nummer, maar eerder een kleine schakel binnen een groot bedrijf"; texts[1]="hier komt een tweede quote van een medewerker"; texts[2]="hier komt een derde quote van een medewerker"; function changetext(){ document.getelementbyid('quote').innerhtml=texts[count]; count++; if(count==texts.length){count='0';} settimeout("changetext()",delay*1000); } </script> <body onload="changetext();"> <h3 id="quote"></h3> 

thanx! annelies

demo

create <img> element , change data structure:

var delay="10"; //how many seconds wnat delay var count=0; var texts = []; texts.push({ image : 'image1.jpg', text : 'je voelt je hier geen nummer, maar eerder' }); texts.push({ image : 'image2.jpg', text : 'je voelt je hier' }); texts.push({ image : 'image3.jpg', text : 'je voelt je hier geen nummer, maa' });  function changetext(){     document.getelementbyid('quote').innerhtml=texts[count].text;     document.getelementbyid('image').src=texts[count].image;     count++;     if(count==texts.length){count=0;}     settimeout(changetext,delay*1000); } 

html:

<h3 id="quote"></h3> <img src="" id="image" /> 

side notes:

  • the [] syntax if preferred creating arrays var texts = []
  • by using texts.push() can append array without having set next key each time
  • don't pass string settimeout, pass function name without quotes or parenthesis instead.
  • when setting count 0, don't wrap in quotes because sets string.

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -