Note that there are some explanatory texts on larger screens.

plurals
  1. POChange html to next item in array - jquery
    primarykey
    data
    text
    <p>My first array, what I thought would be pretty simple just isn't (although my knowledge of js is pretty minimal).</p> <p>I'm trying to iterate through the array (in order) on a click event. So, on the front-end, you'll be presented with a fact and you'll click a button to view the next fact... simple idea.</p> <p>Q: When it's all working, at the end of the array, what will happen when the user clicks to see the next one? How would I go about handling that?</p> <p><strong>JS</strong></p> <pre><code>$(document).ready( function() { function factsInit() { // Define all facts var factText = [ "Oxford won the first Boat Race, which took place on 10th June 1829 at Henley-on-Thames.", "In 2003, Oxford won the closest ever Boat Race by just one foot.", "Oxford coxswain Sue Brown became the first woman to participate in The Boat Race in 1981.", "Oxford's victorious 2009 Blue Boat was the heaviest in the history of the race at an average of 15st 9lb 13oz (99.7kg) per rower.", "In 2012, Oxford's reserve crew, Isis, beat Goldie by 5 lengths to set the course record for the reserve race at 16:41." ], factImage = [ "/clients/oubc/assets/img/factimage_firstrace.jpg", "/clients/oubc/assets/img/factimage_oubc2003.jpg", "/clients/oubc/assets/img/factimage_oubcsuebrown.jpg", "/clients/oubc/assets/img/factimage_oubc2009heaviestever.jpg", "/clients/oubc/assets/img/factimage_isis2012.jpg" ]; // Apply these facts $('#widget_facts .fact_text').html(factText[0]); $('#widget_facts .fact_image &gt; *').attr('src', factImage[0]); // Go to next fact on click $('#widget_facts .link &gt; a').click( function() { $('#widget_facts .fact_text').html(factText++); $('#widget_facts .fact_image &gt; *').attr('src', factImage++); }); } // Run the first fact factsInit(); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload