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


  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


  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)


  17. Cookies set/get/delete

  18. Windows localStorage which fallback to cookies


  19. Trim


  20. getElementsByClassName


  21. Close div/html element when user clicks outside




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


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

  23. Case insensitive sorting of Arrays


    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

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

    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 © 2018 — musings of Aamir Afridi