I have a simple script to hide and show certain buttons, but the javascript doesn't run until a page is reloaded. How can I get it to run as soon as a user clicks that link? For example, have the page already hide certain divs the first time a user links to the page.
var array = [".section-1", ".section-2", ".section-3", ".section-4", ".section-5"];
var cnt = 0;
$(function() {
for(var i = 0; i < 5; i++) {
$(array[i]).hide();
}
$(array[cnt]).show();
$(".previous").hide();
$(".next").click(function () {
if(cnt < 4){
$(array[cnt]).hide();
cnt++;
$(array[cnt]).show();
}
if (cnt == 4) {
$(".next").hide();
}
if(cnt > 0) {
$(".previous").show();
}
});
$(".previous").click(function (){
if(cnt > 0) {
$(array[cnt]).hide();
cnt--;
$(array[cnt]).show();
}
if(cnt < 4){
$(".next").show();
}
if(cnt == 0){
$(".previous").hide();
}
});
});
Here is the manifest file.
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require users