jeudi 8 janvier 2015

Load a function when a checkbox is checked plus remembering it

This is from the YouTube Javascript API as I believe..



function onEndReload() {
ytplayer = new YT.Player('vframe', {
events: {
'onStateChange': function (event) {
if (event.data == 0) {
var iframe = document.getElementById('vframe');
iframe.src = iframe.src
window.location.reload();
};
}
}
});
}


It targets this iframe and when a video ends, it will refresh the page..



<iframe id="vframe" frameborder="0" src="..." allowfullscreen></iframe>


I wanted to load the function onEndReload() when a checkbox is checked while it stays checked AFTER the refresh..



<input type="checkbox" id="auto"/>


How can I achieve that?


Side Note


This worked from an answer I found here but the problem is that it doesn't refresh the second time..



$(function(){
var test = localStorage.input === 'true';
$('#auto').prop('checked', test || false);
});

$('#auto').on('change', function() {
localStorage.input = $(this).is(':checked');
console.log($(this).is(':checked'));
onEndReload();
});


It works but it only checks the first time, reloads, stays checked after the refresh and forgets it the second time, it only works if I uncheck it and then check it..


Apologies in advance, js student here.. Bear with me..





Aucun commentaire:

Enregistrer un commentaire