js
Using jQuery to trigger html onclick event
$("#test_default")[0].onclick();$(window).scroll(function() {
if ($(window).scrollTop() > 100) {
// > 100px from top - show div
}
else {
// <= 100px from top - hide div
}
});Convert li list to select option list
HTML
<div class="sel-box">
<span id='select'>Select</span>
<ul class='toc-odd level-1' id="sel-option">
<li><a href="1">It's finally here</a></li>
<li><a href="2">Improvements</a></li>
<li><a href="3">Handling</a></li>
</ul>
</div>jQuery
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)
Limit text lenght with jquery
$("div.some-area").text(function(index, currentText) {
return currentText.substr(0, 175);
});if you want to add ellipses at the end:
$("div.some-area").text(function(index, currentText) {
return currentText.substr(0, 175)+'...';
});Drupal JS format
(function($){
$(document).ready(function(){
...
});
})(jQuery);<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();
}
});When this method executes, it retrieves the content of location.href, but then jQuery parses the returned document to find the element with divId. This element, along with its contents, is inserted into the element with an ID (divId) of result, and the rest of the retrieved document is discarded.
$("#divId").load(location.href + " #divId>*", "");