How to create an associative array in JavaScript literal notation

JavaScript has no associative arrays, just objects. Even JavaScript arrays are basically just objects, just with the special thing that the property names are numbers (0,1,…). So look at your code first: It’s important to understand that myArray[‘a’] = 200; is identical to myArray.a = 200;! So to start with what you want: You can’t create a JavaScript array and … 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

What is event bubbling and capturing?

Event bubbling and capturing are two ways of event propagation in the HTML DOM API, when an event occurs in an element inside another element, and both elements have registered a handle for that event. The event propagation mode determines in which order the elements receive the event. With bubbling, the event is first captured and … 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 you encode a string to Base64 in JavaScript?

You can use btoa() and atob() to convert to and from base64 encoding. There appears to be some confusion in the comments regarding what these functions accept/return, so… btoa() accepts a “string” where each character represents an 8-bit byte – if you pass a string containing characters that can’t be represented in 8 bits, it will probably break. This isn’t a … Read more