David Beckam
David Beckam

Reputation: 27

Small Issue with triggering a function when a checkbox is checked

I am Stuck with a small issue. Here is Code


<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<input type="checkbox" id="chkPassport" /><span>Check To load </span>
<script> $(function () {
$("#chkPassport").click(function () {
if ($(this).is(":checked")) {
 $("#b-placeholder").show(
   function() {
 $("#b-placeholder").load("https://dl.dropbox.com/s/2v5pn5anpi0rvwv/search%20article.html");
});
} else {
 $("#b-placeholder").hide();
}
});
});

$(document).ready(function(){
 $("#placeholder").click(function(){
 });
});</script>

<div class="lod-notes" id="b-placeholder" style="display:none;"><img   src="https://upload.wikimedia.org/wikipedia/commons/b/b9/Youtube_loading_symbol_1_(wobbly).gif" width="35px"/> </div>

Click Here To Download Html File

The Problem is this: When you open above html file in Chrome everything works fine .When Checkbox is checked it loads the page.but when I open link in loaded page( ie: Google) it opens google but when I press back button the input is still checked but the page is not loaded. In order to load it I have to uncheck the checked input and then recheck the input. How to edit jquery code so that Function trigger ie site loads whenever input is checked state not input clicked to be in checked state

$("#chkPassport").click(function () {

I think this click function is problem.Due to which site loads only when input clicked to checked state not whenever input is in checked state. Hope you got it . I am stuck here.If you answer me you save my day. Thanks In Advance.

Upvotes: 0

Views: 53

Answers (1)

c0drut
c0drut

Reputation: 101

When you came back, you could check on $(document).ready if the checkbox is checked. If so, just load the function like this. Another solution would be to use target blank on links.

$(document).ready(function(){

if($("#chkPassport").is(":checked")) {
$("#b-placeholder").show(function() {
	$("#b-placeholder").load("https://dl.dropbox.com/s/2v5pn5anpi0rvwv/search%20article.html");
});
}

$("#placeholder").click(function(){});
});

Upvotes: 2

Related Questions