• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details

Show a custom context menu at clicked position

Show a custom context menu at clicked position

Sometimes, we want to replace the default right-click menu with our own menu that allows user to perform additional actions.

Miscellaneous

Add characters to the end of a string if it’s less than a certain length

Add characters to the end of a string if it’s less than a certain length

Accepts two arguments: the length the string should be, and what characters to add if it’s not that length.

Miscellaneous

Find a Specific Element from an Array

Find a Specific Element from an Array

Use find() method to find an element matching a specific criterion

Miscellaneous

Check if a date is the same as another date

Check if a date is the same as another date

Use Date.prototype.toISOString() and strict equality checking (===) to check if the first date is the same as the second one.

Time & Date

Scale a text to fit inside of an element

Scale a text to fit inside of an element

First of all, we need to measure the width of element with its current font size and text content.

Text Effects

Add characters to the beginning of a string if it’s less than a certain length

Add characters to the beginning of a string if it’s less than a certain length

Accepts two arguments: the length the string should be, and what characters to add if it’s not that length.

Miscellaneous

Short-Circuit Evaluation Using &&

Short-Circuit Evaluation Using &&

Instead of having to check if something is true with an if statement, you can use the && operator:

Miscellaneous

Calculate the midpoint between two pairs of (x,y) points

Calculate the midpoint between two pairs of (x,y) points

Destructure the array to get x1, y1, x2 and y2. Calculate the midpoint for each dimension by dividing the sum of the two endpoints by 2.

Math Related

Resize the width of a text box to fit its content automatically

Resize the width of a text box to fit its content automatically

To adjust its width based on its content dynamically, we create a fake element whose content is the same as the input value.

CSS

Check if an element has an attribute

Check if an element has an attribute

Check for the existence of an attribute (including data attributes) on an element. This method can also be used to manipulate other types of attributes—things like id, tabindex, name, and so on.

CSS

  • « Go to Previous Page
  • Go to page 1
  • Interim pages omitted …
  • Go to page 15
  • Go to page 16
  • Go to page 17
  • Go to page 18
  • Go to page 19
  • Interim pages omitted …
  • Go to page 36
  • Go to Next Page »

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers