Web & Software Developer

Archive for “JavaScript Code Snippets”

JavaScript code snippets and quick JavaScript (js) tips for customizing your own website via this browser-side language.

Simple Show/Hide Toggle With Pure JavaScript

This is how to toggle showing and hiding an element with pure JavaScript. Here is a working example. In this example, the item that gets clicked to toggle has the class “click-to-toggle”. When this is clicked, the list underneath is will be shown. When the heading is clicked again, the list will be hidden. The […]

Set Style Attribute For Multiple Elements of a Class With JavaScript

This is how to use the .setAttribute() JavaScript method to apply to multiple elements on a page. This uses pure JavaScript, no jQuery or other libraries. In this example, we are targeting all elements with the class “my-class”. This will add an inline style of “cursor:pointer” to all the elements with the “.my-class” class.

Toggle Show/Hide with jQuery

This is for when you need to toggle showing and hiding more than one element on a page. For example, a page full of headings in which you click a heading to show a list underneath it, then click again to hide the list. This uses jQuery. (See this instead for a pure JavaScript example.) […]

Toggle Show/Hide Multiple Elements With Pure JavaScript

This is for when you need to toggle between showing and hiding more than one element on a page. For example, click a heading to show a list underneath it, then click again to hide the list. This uses pure JavaScript, no jQuery or other libraries. (See this instead for an example using jQuery.) You […]

Detecting Touch Pointer Events With jQuery on Windows Phone 8.1, IE 11

You may be using jQuery to detect pointer events, such as ‘touchstart’ or ‘MSPointerDown’, for example, for toggling a mobile menu when the user touches a button on a mobile touch-screen device. The ‘MSPointerDown’ event for Windows phones and Internet Explorer would have worked in IE 10, but not in IE 11. Likewise, for many […]

JavaScript: Convert Latitude/Longitude Decimal to Degrees, Minutes

JavaScript function to convert latitude/longitude decimal coordinates into a format like “80w25”. It will show degrees, then the cardinal direction letter (s for south, n for north, w for west, e for east), then the minutes. In other words, this converts longitude/latitude from DDD format into DMS format, but without seconds. This is pure JavaScript, […]

Validate a Contact Form With jQuery

This example is a jQuery validation script for a contact form. It validates required input text fields, required select fields, checks that an email address was entered, and checks that the 2nd email address entered matches the first. You must edit lines 3, 6, and 62 to match your own id’s and field names. For […]