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

Validate decimal numbers in JavaScript – IsNumeric()

@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

Generate random string/characters in JavaScript

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));

Set a default parameter value for a JavaScript function

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

Adding a table row in jQuery

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

Scroll to an element with jQuery

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

error code: 521