Jquery if scroll is a certain amount of pixels

You can check $(document).scrollTop() inside of a scroll handler: var $document = $(document), $element = $(‘#some-element’), className=”hasScrolled”; $document.scroll(function() { if ($document.scrollTop() >= 50) { // user scrolled 50 pixels or more; // do stuff $element.addClass(className); } else { $element.removeClass(className); } }); If adding the class name is all you want (no other actions needed), this … Read more

Format input value in Angularjs

Here is working example where we use unshift: angular.module(‘myApp.directives’, []).directive(‘format’, [‘$filter’, function ($filter) { return { require: ‘?ngModel’, link: function (scope, elem, attrs, ctrl) { if (!ctrl) return; ctrl.$formatters.unshift(function (a) { return $filter(attrs.format)(ctrl.$modelValue) }); ctrl.$parsers.unshift(function (viewValue) { var plainNumber = viewValue.replace(/[^\d|\-+|\.+]/g, ”); elem.val($filter(attrs.format)(plainNumber)); return plainNumber; }); } }; }]); The HTML seems: <input type=”text” ng-model=”test” … Read more

Rails flash notice via ajax

Here is an example that I got working, thanks to Rich Peck’s answer. I needed to use flash.now to make sure the flash notice didn’t persist. AJAX trigger in the view: <%= link_to “Email report”, users_path, remote: true %> Controller: # app/controllers/users_controller class UsersController < ApplicationController def index # do some things here respond_to do … Read more

jQuery: height()/width() and “display:none”

If an element has an offsetWidth of 0 (jQuery is considering this “hidden”), checked here, then it attempts to figure out what the height should be. It sets the following properties on the element via jQuery.swap() for the duration of the check: position: “absolute” visibility: “hidden” display: “block” Then it gets the height, via getWidthOrHeight(…) … Read more

How to keep line breaks when using .text() method for jQuery?

You don’t need to worry about the HTML entities nor any complex string replacing. All you need is a little CSS: #target { white-space: pre; } and use the .text() approach: (function(){ var srcText = $(“#src”).text().trim(); i = 0; result = srcText[i]; setInterval(function() { if(i == srcText.length-1) { clearInterval(this); return; }; i++; result += srcText[i]; … Read more

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