Saturday, May 2, 2015

Clear Default Text using JAVASCRIPT

Many web designers like to add some default text to form inputs like text boxes, to signify what the user should type into the box. When the user clicks the input, this default is removed so they can begin typing. This can be a nice usability improvement when used correctly. Below you’ll find a nice, clean way to approach it, that requires minimal work on your part to get it working.

The HTML

On this page we’re going to work through an example of a single text box that comes with some default text. When the user clicks on the box, the default text is wiped away so that they can begin typing. If they click away from the box, without typing anything in, we will add the default text back so that they don’t forget what was meant to be typed. Check it out below:
Creating this text box is easy, we simply add the default text through the input’s valueattribute, like so:
Enter a date: <input type="text" name="date" value="yy-mm-dd">
This default — known as a “representative value” — suggests that in this case our form is expecting a date in the format “yy-mm-dd” (two numbers for the year, two for the month, and two for the day; for example “05-11-22”). This is very useful for users, since they will all have different preferences for how to enter dates. Coding all of these possibilities into your form-processing code can be a nightmare, so this way you can guide your users to enter the date in the preferred format, and save yourself the trouble.

The JavaScript

The magic part in all of this is accomplished through a sprinkle of JavaScript. You should have some knowledge of event handlers and working with forms to understand what happens next. This script is going to be made » unobtrusive, meaning you can add the JavaScript file to any page and it will just work without having to make big changes to the HTML of the page.
The setup of our JavaScript is as so:
  1. When the page loads, we will save the value of the input as a property of the element, so we can use it later.
  2. When the user clicks (or otherwise focuses) on the element, we check if the current value is the same as this saved default text. If it is, we clear the input. Otherwise we leave it as it is.
  3. When the user clicks away from the element, we check if they’ve filled anything into the text box. If they have, we do nothing. If they’ve left it blank, we’ll fill back in the default text.
This nicely breaks down into three JavaScript functions. First of all, we’re going to need myutil-functions.js file, which provides a slew of general-use functions. (Internet Explorer users, open the .js files with Notepad.) Import that file and this new clear-default-text.jsfile and you’re good to go. Save those two files among your other website files, and add this to the <head> of any pages you need it in:
<script type="text/javascript" src="util-functions.js"></script>
<script type="text/javascript" src="clear-default-text.js"></script>
Finally, and very importantly, for any text inputs that you want this to work on, you need to give them a special class, like this:
<input type="text" name="date" value="yy-mm-dd" class="cleardefault">
Our script checks for the existance of this class in order to work its magic. Those who want to understand how the code works can read on for the full explanation...

Explaining the JavaScript

So, our first function runs when the page loads, and finds all the input elements on the page. If the input has the type “text” (i.e. it’s a one-line text input), and it has the class “cleardefault,” we add event handlers for the focus and blur events.
addEvent(window, 'load', init, false);

function init() {
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        
        if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {  
            /* Add event handlers */          
            addEvent(theInput, 'focus', clearDefaultText, false);
            addEvent(theInput, 'blur', replaceDefaultText, false);
Then we save the input’s current value into a new property that we’re creating, calleddefaultText. This makes use of JavaScript’s handy ability to add arbitrary properties to any element. We simply make up a new one so we can store this information as part of the element.
            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
}
So, now our inputs are set up, we need the function that run when the focus and blur events occur. They’re pretty simple. First, in both, we have to find the target element that actually fired the event. Once we have this, we check to see if the user has interacted with this input before, and react accordingly.
function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}
And that’s all there is to it. This script can be dropped into any page, and as long as the inputs on the page have been given the right class, it’ll work. Enjoy.

Friday, May 1, 2015

65+ Best Free Javascript Tutorials, PDF & eBooks For Web Developers

Javascript Tutorials
JavaScript has gained a lot of popularity as one of the main programming languages for the development of highly interactive web apps. This language has become an important factor for both web app developers as well as browser vendors. Owing to the increasing browser functionality, in terms of a greater amount of interpreter and compiler technology, JavaScript has become the bloodline of almost every organization. So, if you are looking forward to become a part of this much coveted IT sector, JavaScript is a must learn language for you.

A well used Javascript application will enable your pages to be more dynamic and be highly interactive with the end user with minimal server side interaction. Many recent server side applications and tools have been developed keeping Javascript as the client in mind. At the same time, there are dozens of MVC frameworks that have evolved using only Javascript. This has made web front end development must fast and very interactive. 

You may find a lot of good books on JavaScript and some books are aimed on AJAX, however in this post we have focused on different kinds of free web resources which will help you learn JavaScript online for free are:


Free eBooks And Noteworthy Online Tutorials

If you like to learn by reading, you will find plenty of free e-books which will take you through the various aspects of JavaScript language. Regardless of whether you are a beginner or an expert programmer, you will find an e-book which suits your needs the best.
  1. Eloquent JavaScript A Modern Introduction to Programming
  2. Essential JS Design Patterns eBook
  3. JS eBook by InformIT
  4. It eBooks - A good collection of Free JavaScript eBooks with a lot of other useful programming books as well.
  5. Introduction To JavaScript Unit Testing
  6. Which JavaScript Recipe Is Right For You?
    This is a javascript tutorial for beginners.
  7. Building A Relationship Between CSS & JavaScript. This is a javascript tutorial for web designing using JavaScript.
  8. Writing Fast, Memory-Efficient JavaScript
  9. Make your JavaScript apps smoother
  10. A-Z of JavaScript
  11. Introduction to HTML5 Web Workers
  12. Net Magazine JavaScript Tutorials
  13. Design Patterns in Javascript - by TutsPlus
  14. Principles of Maintainable JavaScript - By TutsPlus

Free Websites

In case, you are beginning to learn JavaScript, there can be nothing better than seeking the help of an online website. These online websites are available free of cost and are easier to access from everywhere, provided you have internet.
  1. Mozilla Developer Network - Developer Mozilla JS Guide
  2. W3cSchool
  3. Ajaxian
  4. Interactive Javascript tutorial
  5. Javascript tutorial by codecademy
  6. Nathans Lessons
  7. JavaScript Tutorial By Knockoutjs
  8. JavaScript info
  9. JavaScript Guide By HTMLDog
  10. JavaScript Bookon WikiBooks
  11. JS Tutorial on Javascript Kit
  12. Echo echo Javascript
  13. Tutorialized - JS Tutorial
  14. Tutorialspoint
  15. Html Goodies
  16. Pagere Source JS
  17. JS Tutorial by Brown University
  18. Web Monkey - Advanced JS Tutorial
  19. HTML.net
  20. Microsoft App using JS- Create your first Windows Store app using JavaScript
  21. Good Tutorials
  22. JS Made Easy
  23. AppendTo
  24. Your HTML Source
  25. Site Point JS Tutorial

Free Video Tutorials

It can be quite difficult for some people to learn JavaScript on their own. However, you can easily watch one of the online video tutorials which teach best practices, right from the beginning. Everything from basic to the advanced concepts is covered in these online tutorials.
  1. YUI Blog - Douglas Crockford is Yahoo!'s JavaScript architect
  2. Some more Video tutorials By Douglas Crockford Yahoo!'s JavaScript architect.
  3. The Good Parts
  4. A JavaScript video tutorial series
  5. JavaScript Tutorialsby newboston
  6. Learn everything there is to know about JavaScript - Document Object Model : Dynamic Styling : AJAX : Form Validation
  7. JS Tutorials Playlist on Youtube by Chris Walker
  8. Introduction to JavaScript and Browser DOMBy GoogleTechTalks
  9. Javascript Tutorials Compilation by FromDev.com For Beginners Playlist - A Playlist of some hidden great javascript tutorials that you can use to learn and expert JS programmer.
  10. Introduction to Javascript Programming
  11. Speed Up Your JavaScript - By Google Tech Talks
  12. Douglas Crockford: Advanced JavaScript
  13. Introduction to JavaScript and HTML5 Canvas
  14. JS Tutorials Playlistby Adam Khoury
  15. Video Tutorials Playlist on Advanced JavaScript programming including various tips & techniques

Best Free Cheat Sheets & PDF For JavaScript

JavaScript cheat sheets will act as your quick reference guide. It is designed in a manner so as to act like a reminder sheet, listing the various functions and methods of JavaScript so that you can never go wrong with its usage.
  1. Added Bytes Cheatsheet on JavaScript
  2. JS Reference Booklet
    This ebook can serve as a really useful cheatsheet and Javascript tutorial PDF for beginners (download free ).
  3. Javascript.su
  4. Javascript Reference
  5. JS FAQs
  6. Cheatsheet for Prototype JS Library. This is a quick javascript reference PDF for web designing using Prototype JS library.


Best Free Web Forums To Ask Questions


Only learning the language on your own isn’t enough. You must become part of one of the JavaScript web forums where constant discussions pertaining to JavaScript and other related technologies take place.
  1. Stackoverflow - I find this to be the best place for Javascript related questions and answers. Make sure to tag your questions appropriately and search for similar questions before posting.
  2. Coding Forums JavaScript
  3. Code Project JavaScript
  4. Web Developer JavaScript
  5. HTML Help Centeral

Some Informative And Useful JavaScript Blogs


There are a lot of other websites and blogs created by JS experts that you may 
  1. Badassjs
  2. Microsoft JS Development Blog
  3. Web Reflection

So, whether you are simply starting out, want to enhance your skills or are quite an experienced JavaScript professional, these resources will help everyone grab something worthwhile.