Download File Using JavaScript/jQuery

Use an invisible <iframe>: To force the browser to download a file it would otherwise be capable of rendering (such as HTML or text files), you need the server to set the file’s MIME Type to a nonsensical value, such as application/x-please-download-me or alternatively application/octet-stream, which is used for arbitrary binary data. If you only want to open it in a … Read more

Disabling and enabling a html input button

Using Javascript Disabling a html buttondocument.getElementById(“Button”).disabled = true; Enabling a html buttondocument.getElementById(“Button”).disabled = false; Demo Here Using jQuery All versions of jQuery prior to 1.6 Disabling a html button$(‘#Button’).attr(‘disabled’,’disabled’); Enabling a html button$(‘#Button’).removeAttr(‘disabled’); Demo Here All versions of jQuery after 1.6 Disabling a html button$(‘#Button’).prop(‘disabled’, true); Enabling a html button$(‘#Button’).prop(‘disabled’, false); Demo Here P.S. Updated … Read more

jQuery setTimeout() Function [duplicate]

The problem is that inside the setTimeout() call, this doesn’t refer to the button. You need to set a variable to keep the reference to the button. I’ve created a sample below. See how I use the variable named $this. UPDATE: Now with modern browsers supporting Arrow Functions, you can use them to avoid altering the this context. See updated snippet below.

jQuery.click() vs onClick

Using $(‘#myDiv’).click(function(){ is better as it follows standard event registration model. (jQuery internally uses addEventListener and attachEvent). Basically registering an event in modern way is the unobtrusive way of handling events. Also to register more than one event listener for the target you can call addEventListener() for the same target. http://jsfiddle.net/aj55x/1/ Why use addEventListener? (From MDN) addEventListener is the way to register an event listener as specified in W3C DOM. Its … Read more

How can I scroll to an element using jQuery?

JavaScript answer If you would like to scroll to an element smoothly with “pure JavaScript” you could do something like this: More information is here: Element.scrollIntoView() NOTE: Please see Can I use… Support tables for HTML5, CSS3, etc. for the latest browser support… jQuery answer If you would like to scroll to an element smoothly using jQuery then … Read more