Remove duplicate values from JS array [duplicate]

TL;DR Using the Set constructor and the spread syntax: uniq = […new Set(array)]; “Smart” but naïve way uniqueArray = a.filter(function(item, pos) { return a.indexOf(item) == pos; }) Basically, we iterate over the array and, for each element, check if the first position of this element in the array is equal to the current position. Obviously, … Read more

How do I format a Microsoft JSON date?

eval() is not necessary. This will work fine: var date = new Date(parseInt(jsonDate.substr(6))); The substr() function takes out the /Date( part, and the parseInt() function gets the integer and ignores the )/ at the end. The resulting number is passed into the Date constructor. I have intentionally left out the radix (the 2nd argument to … Read more

$(document).ready equivalent without jQuery

There is a standards based replacement,DOMContentLoaded that is supported by over 99% of browsers, though not IE8: document.addEventListener(“DOMContentLoaded”, function(event) { //do work }); jQuery’s native function is much more complicated than just window.onload, as depicted below. function bindReady(){ if ( readyBound ) return; readyBound = true; // Mozilla, Opera and webkit nightlies currently support this … Read more

Get the size of the screen, current web page and browser window

You can get the size of the window or document with jQuery: // Size of browser viewport. $(window).height(); $(window).width(); // Size of HTML document (same as pageHeight/pageWidth in screenshot). $(document).height(); $(document).width(); For screen size you can use the screen object: window.screen.height; window.screen.width;

How to get the children of the $(this) selector?

The jQuery constructor accepts a 2nd parameter called context which can be used to override the context of the selection. jQuery(“img”, this); Which is the same as using .find() like this: jQuery(this).find(“img”); If the imgs you desire are only direct descendants of the clicked element, you can also use .children(): jQuery(this).children(“img”);

.prop() vs .attr()

Update 1 November 2012 My original answer applies specifically to jQuery 1.6. My advice remains the same but jQuery 1.6.1 changed things slightly: in the face of the predicted pile of broken websites, the jQuery team reverted attr() to something close to (but not exactly the same as) its old behaviour for Boolean attributes. John … Read more

Disable/enable an input with jQuery?

jQuery 1.6+ To change the disabled property you should use the .prop() function. $(“input”).prop(‘disabled’, true); $(“input”).prop(‘disabled’, false); jQuery 1.5 and below The .prop() function doesn’t exist, but .attr() does similar: Set the disabled attribute. $(“input”).attr(‘disabled’,’disabled’); To enable again, the proper method is to use .removeAttr() $(“input”).removeAttr(‘disabled’); In any version of jQuery You can always rely … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)