Check if a variable is a string in JavaScript
This is what works for me: if (typeof myVar === ‘string’ || myVar instanceof String) // it’s a string else // it’s something else
This is what works for me: if (typeof myVar === ‘string’ || myVar instanceof String) // it’s a string else // it’s something else
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
You can use the qualities of the abstract equality operator to do this: if (variable == null){ // your code here. } Because null == undefined is true, the above code will catch both null and undefined.
@Joel’s answer is pretty close, but it will fail in the following cases: // Whitespace strings: IsNumeric(‘ ‘) == true; IsNumeric(‘\t\t’) == true; IsNumeric(‘\n\r’) == true; // Number literals: IsNumeric(-1) == false; IsNumeric(0) == false; IsNumeric(1.1) == false; IsNumeric(8e5) == false; Some time ago I had to implement an IsNumeric function, to find out if … Read more
I think this will work for you: function makeid(length) { var result=””; var characters=”ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789″; var charactersLength = characters.length; for ( var i = 0; i < length; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } console.log(makeid(5));
The Date object will do what you want – construct one for each date, then compare them using the >, <, <= or >=. The ==, !=, ===, and !== operators require you to use date.getTime() as in var d1 = new Date(); var d2 = new Date(d1); var same = d1.getTime() === d2.getTime(); var … Read more
Sure the code does work, but I’m pretty sure it doesn’t do what you expect it to do. It just fires off multiple asynchronous calls, but the printFiles function does immediately return after that. Reading in sequence If you want to read the files in sequence, you cannot use forEach indeed. Just use a modern … Read more
From ES6/ES2015, default parameters are in the language specification. function read_file(file, delete_after = false) { // Code } just works. Reference: Default Parameters – MDN Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed. In ES6, you can simulate default named parameters via destructuring: // … Read more
The approach you suggest is not guaranteed to give you the result you’re looking for – what if you had a tbody for example: <table id=”myTable”> <tbody> <tr>…</tr> <tr>…</tr> </tbody> </table> You would end up with the following: <table id=”myTable”> <tbody> <tr>…</tr> <tr>…</tr> </tbody> <tr>…</tr> </table> I would therefore recommend this approach instead: $(‘#myTable tr:last’).after(‘<tr>…</tr><tr>…</tr>’); … Read more
Assuming you have a button with the id button, try this example: $(“#button”).click(function() { $([document.documentElement, document.body]).animate({ scrollTop: $(“#elementtoScrollToID”).offset().top }, 2000); }); I got the code from the article Smoothly scroll to an element without a jQuery plugin. And I have tested it on the example below. <html> <script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js”></script> <script> $(document).ready(function (){ $(“#click”).click(function (){ $(‘html, … Read more