javascript - $(document).ready(function() vs .click(function() -


i have page populated $.getjson() function working perfectly, json being written dom correctly have function toggles display of elements on page if there matching value in query string.

$(document).ready(function () {     $("#one").toggle(data.indexof("one") !== -1);     $("#two").toggle(data.indexof("two") !== -1);     $("#three").toggle(data.indexof("three") !== -1);     $("#four").toggle(data.indexof("four") !== -1); }); 

if use other event fire function correct elements hidden/displayed on page example.

$(document).click(function () {     $("#one").toggle(data.indexof("one") !== -1);     $("#two").toggle(data.indexof("two") !== -1);     $("#three").toggle(data.indexof("three") !== -1);     $("#four").toggle(data.indexof("four") !== -1); }); 

can please explain why .ready() function not working?

interestingly loading content of page $.getjson() function seems have because if hard code html .ready() function works.

cheers jeff

the ready event fired when dom page loaded.

your getjson() function triggered after dom loaded !

add promise getjson code change it.

.getjson().then(function() {    $("#one").toggle(data.indexof("one") !== -1);    $("#two").toggle(data.indexof("two") !== -1);    $("#three").toggle(data.indexof("three") !== -1);    $("#four").toggle(data.indexof("four") !== -1); }); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -