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 … Read more →

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.) … Read more →

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 … Read more →