Length of a JavaScript object

Updated answer Here’s an update as of 2016 and widespread deployment of ES5 and beyond. For IE9+ and all other modern ES5+ capable browsers, you can use Object.keys() so the above code just becomes: var size = Object.keys(myObj).length; This doesn’t have to modify any existing prototype since Object.keys() is now built-in. Edit: Objects can have … Read more

Is there a standard function to check for null, undefined, or blank variables in JavaScript?

You can just check if the variable has a truthy value or not. That means if( value ) { } will evaluate to true if value is not: null undefined NaN empty string (“”) 0 false The above list represents all possible falsy values in ECMA-/Javascript. Find it in the specification at the ToBoolean section. … Read more

How can I know which radio button is selected via jQuery?

To get the value of the selected radioName item of a form with id myForm: $(‘input[name=radioName]:checked’, ‘#myForm’).val() Here’s an example: $(‘#myForm input’).on(‘change’, function() { alert($(‘input[name=radioName]:checked’, ‘#myForm’).val()); }); <script src=”https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js”></script> <form id=”myForm”> <fieldset> <legend>Choose radioName</legend> <label><input type=”radio” name=”radioName” value=”1″ /> 1</label> <br /> <label><input type=”radio” name=”radioName” value=”2″ /> 2</label> <br /> <label><input type=”radio” name=”radioName” value=”3″ /> … Read more

How can I merge properties of two JavaScript objects dynamically?

ECMAScript 2018 Standard Method You would use object spread: let merged = {…obj1, …obj2}; merged is now the union of obj1 and obj2. Properties in obj2 will overwrite those in obj1. /** There’s no limit to the number of objects you can merge. * Later properties overwrite earlier properties with the same name. */ const … Read more

Why does my JavaScript code receive a “No ‘Access-Control-Allow-Origin’ header is present on the requested resource” error, while Postman does not?

If I understood it right you are doing an XMLHttpRequest to a different domain than your page is on. So the browser is blocking it as it usually allows a request in the same origin for security reasons. You need to do something different when you want to do a cross-domain request. A tutorial about … Read more

Is it possible to apply CSS to half of a character?

Now on GitHub as a Plugin! Feel free to fork and improve. Demo | Download Zip | Half-Style.com (Redirects to GitHub) Pure CSS for a Single Character JavaScript used for automation across text or multiple characters Preserves Text Accessibility for screen readers for the blind or visually impaired Part 1: Basic Solution Demo: http://jsfiddle.net/arbel/pd9yB/1694/ This … Read more

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