5

JS/jQuery Code Snippets

Share Button

Some common Javascript snippets I use in my projects. If you got something useful, please share in comments section.

  1. Check if element has scrollbar

    I was working on a project where I need to add jQuery UI resizable to a list but I have to check if it has a scrollbar. I endup with a tiny jQuery plugin

    Usage

  2. setInterval and setTimeout

    Whenever you use setInterval make sure you clear it just incase if it never gets cleared.

  3. Strip out duplicate items in an Array

    Usage

  4. Check if a value is in an Array

  5. Get a random item from an Array

  6. Shuffle an array of numbers

  7. Make text Unselectable (no copy paste allowed)

  8. Check if number has decimal

  9. Check if URL exists

  10. Duplicating an Array (an interview question)

    Make this work: [1,2,3].duplicate();

  11. Giving width to a UL/OL when LIs are displayed horizontally using jQuery

  12. RGB to HEX

  13. HEX To RGB

  14. Area To Radius

  15. Quick ie6 PngFix using jQuery

  16. Date difference (object & formated)

    Usage

  17. Cookies set/get/delete

  18. Windows localStorage which fallback to cookies

    Usage

  19. Trim

    Usage

  20. getElementsByClassName

    Usage

  21. Close div/html element when user clicks outside

    HTML:

    JS:

    Demo:

  22. Add/Remove/Toggle and hasClass / CSS classes manipulation

    Usage/demo:

    Demo: http://jsfiddle.net/aamir/fbD5j/

  23. Case insensitive sorting of Arrays

    Usage

    If the contents of the array are say [a,B,A,z,e,C], then the resultant array shall be some thing like this, [a,A,B,C,e,z]

  24. Some jQuery patterns

    Using $.fn

    Pass jQuery to the plugin to be safe

    Add more than one methods

    My favorite

Please feel free to add your common tiny jQuery plugins or Javascript methods you use in your daily life in comments section below.

Share Button
  • http://www.mirnauman.wordpress.com Mir Nauman Tahir

    nice one dude, i ll need some of the above for my upcoming project

    • http://www.aamirafridi.com Aamir Afridi

      Thanks, glad you liked it :)

  • http://www.arbisoft.com Mahboob Ur Rasheed

    Great. Thanks for Sharing.

  • mahdiaction

    hello
    are you an iranian?
    your family is like persian family.
    Tanks For Your Codes,
    I used your code.

    salam
    shoma irani hastid?
    mamnoon az source hatoon .
    man az codatoon estefade kardam.

    خیلی ممنونم از شما

    • http://www.aamirafridi.com Aamir Afridi

      Hi Mahdiaction,

      No I am not iranian :)

      Thanks for liking my codes :P

Copyright © 2017 — musings of Aamir Afridi