You are here

js

Set cookie without plugin

function setCookie(key, value, days) {
            var expires = new Date();
            if (days) {
                expires.setTime(expires.getTime() + (days * 24 * 60 * 60 * 1000));
                document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
            } else {
                document.cookie = key + '=' + value + ';expires=Fri, 30 Dec 9999 23:59:59 GMT;';
            }
        }

        function getCookie(key) {

Add string after n values

jQuery(function($){

setInterval(function() {
$('.wc-credit-card-form-card-expiry').on('keyup',keyUpHandler);
   
    function keyUpHandler(e){ 
        var element = this;
        var key = e.keyCode || e.which;  
        insertTimingColor(element,key)
    }

    function insertTimingColor(element,key){
        var inputValue = element.value;
        if(element.value.trim().length == 2 && key !== 4){
            element.value = element.value + '/';
        }
    }
}, 100)

Check for element to appears in the DOM and do stuff and stop interval

<script>
jQuery(function($){
myVar = setInterval(function() {
if ($('.x-class').length) {
    $('.contact-form-home .et-pb-contact-message li:last-child:not(.appearedx)')
        .addClass('appearedx')
        .each(function() {
            $(".appearedx").text('Math Problem');
        });
    clearInterval(myVar);
    }
}, 100)
});
</script>

myVar = setInterval("javascript function", milliseconds);

Remove datetime older than "12 month" (ex)

    jQuery('.editions-wrapper .posts-list li').each(function() {
        var d1 = jQuery(this).find('time').attr('datetime');
        var d2 = new Date();
        var m = new Date(d1);
        var timeDiff = Math.abs(d2.getTime() - m.getTime());
        var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
           if ( diffDays >= 365) {
               jQuery(this).remove();
           }       
     });

Pages