How do I set/unset a cookie with jQuery?

Update April 2019 jQuery isn’t needed for cookie reading/manipulation, so don’t use the original answer below. Go to https://github.com/js-cookie/js-cookie instead, and use the library there that doesn’t depend on jQuery. Basic examples: // Set a cookie Cookies.set(‘name’, ‘value’); // Read the cookie Cookies.get(‘name’) => // => ‘value’ See the docs on github for details. Before … Read more

jQuery document.createElement equivalent?

Here’s your example in the “one” line. this.$OuterDiv = $(‘<div></div>’) .hide() .append($(‘<table></table>’) .attr({ cellSpacing : 0 }) .addClass(“text”) ) ; Update: I thought I’d update this post since it still gets quite a bit of traffic. In the comments below there’s some discussion about $(“<div>”) vs $(“<div></div>”) vs $(document.createElement(‘div’)) as a way of creating new … Read more

How to check if element is visible after scrolling?

This should do the trick: function isScrolledIntoView(elem) { var docViewTop = $(window).scrollTop(); var docViewBottom = docViewTop + $(window).height(); var elemTop = $(elem).offset().top; var elemBottom = elemTop + $(elem).height(); return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop)); } Simple Utility Function This will allow you to call a utility function that accepts the element you’re looking … Read more

How to find the sum of an array of numbers

This’d be exactly the job for reduce. If you’re using ECMAScript 2015 (aka ECMAScript 6): const sum = [1, 2, 3].reduce((partialSum, a) => partialSum + a, 0); console.log(sum); // 6 For older JS: const sum = [1, 2, 3].reduce(add, 0); // with initial value to avoid when the array is empty function add(accumulator, a) { return accumulator … Read more

How to change the href attribute for a hyperlink using jQuery

Using $(“a”).attr(“href”, “http://www.google.com/”) will modify the href of all hyperlinks to point to Google. You probably want a somewhat more refined selector though. For instance, if you have a mix of link source (hyperlink) and link target (a.k.a. “anchor”) anchor tags: <a name=”MyLinks”></a> <a href=”http://www.codeproject.com/”>The CodeProject</a> …Then you probably don’t want to accidentally add href … Read more

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