New course! Every coder should learn Generative AI!
Try a free lesson+ 1
Vanilla version of this jQuery
Is there a vanilla javascript version of this jQuery: setInterval(function() { if ($('#cursor').css('visibility') === 'visible') { $('#cursor').css({ visibility: 'hidden' }) } else { $('#cursor').css({ visibility: 'visible' }) } }, 500); I tried this but didn't work: setInterval(function(){ if(cursor.style.visibility="visible"){ cursor.style.visibility="hidden"; } else{ cursor.style.visibility="visible"; } },500); Neither did this one: setInterval(function(){ if(cursor.style.display="block"){ cursor.style.display="none"; } else{ cursor.style.display="block"; } },500); Is it even possible to use pure javascript to get the same blinking effect? Both of my attempts only make it blink once.
3 Answers
+ 5
Shadow is spot on
By assigning the value instead of comparing then the first condition is always met
Anyways, anything which is possible with jQuery is possible with vanilla js
jQuery is implemented with js after all....
and for future, it will be much easier if you attached a code link for others to play with.
it will be so much more convenient ;)
+ 4
Thank you for the insights, guys! :D
+ 3
Maybe it didn't work because you used the assignment operator = in the if-cases instead of the comparison operator == or ===?
Also, if cursor is an id, then you will have to retrieve it first using
document.getElementById('cursor') instead of only writing cursor.