Publishing jQuery Plugins to npm

Publishing jQuery plugins to npm involves several steps to ensure that your plugin is properly packaged, documented, and easily installable for other developers. Here’s a step-by-step guide on how to publish your jQuery plugin to npm: link 1. Create Your jQuery PluginWrite your jQuery plugin code and make sure it follows best practices. Include comments … Continue reading

How do I get the text value of a selected option?

Select elements typically have two values that you want to access. First there’s the value to be sent to the server, which is easy: 1 2 $( “#myselect” ).val();// => 1 The second is the text value of the select. For example, using the following select box: 1 2 3 4 5 6 7 <select … Continue reading

How do I pull a native DOM element from a jQuery object?

A jQuery object is an array-like wrapper around one or more DOM elements. To get a reference to the actual DOM elements (instead of the jQuery object), you have two options. The first (and fastest) method is to use array notation: 1 $( “#foo” )[ 0 ]; // Equivalent to document.getElementById( “foo” ) The second … Continue reading

How do I test whether an element exists?

Use the .length property of the jQuery collection returned by your selector: 1 2 3 4 5 if ( $( “#myDiv” ).length ) { $( “#myDiv” ).show(); } Note that it isn’t always necessary to test whether an element exists. The following code will show the element if it exists, and do nothing (with no … Continue reading

$( document ).ready()

A page can’t be manipulated safely until the document is “ready.” jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Code included inside $( window ).on( “load”, function() { … }) will run … Continue reading

Iterating over jQuery and non-jQuery Objects

jQuery provides an object iterator utility called $.each() as well as a jQuery collection iterator: .each(). These are not interchangeable. In addition, there are a couple of helpful methods called $.map() and .map() that can shortcut one of our common iteration use cases. link $.each()$.each() is a generic iterator function for looping over object, arrays, … Continue reading

The jQuery Object

When creating new elements (or selecting existing ones), jQuery returns the elements in a collection. Many developers new to jQuery assume that this collection is an array. It has a zero-indexed sequence of DOM elements, some familiar array functions, and a .length property, after all. Actually, the jQuery object is more complicated than that. link … Continue reading

Manipulating Elements

For complete documentation of jQuery manipulation methods, visit the Manipulation documentation on api.jquery.com. link Getting and Setting Information About ElementsThere are many ways to change an existing element. Among the most common tasks is changing the inner HTML or attribute of an element. jQuery offers simple, cross-browser methods for these sorts of manipulations. You can … Continue reading