1. BASICS
- Initialization
- Write to HTML
- Manipulating DOM
2. FORMS
- Validate form
- Interact with form
3. Setting visibility of elements
4. AJAX
5. Object
6. Knockout data binding
Tutorial on javascript-coder.com
1. BASICS
Code example:Html with JavaScript
Initialization
JavaScript function initialize() { // do something }
HTML <body onload="initialize()">
Write to HTML
JavaScript function doSomething() { document.write("Hello"); }
Manipulating DOM
HTML <input id="edit1" type="text" value="abc" />
JavaScript var label = document.getElementById("myId"); var edit = document.getElementById("edit1"); alert( "Text value is: " + label.innerHTML + ", Edit box value is: " + edit.value ); edit.disabled=true; // disable edit box
2. FORMS
Code example:Form with JavaScript
Validate form
JavaScript function validateForm() { var result = .... return result; }
HTML <FORM action="form_response.htm" method="post" onSubmit="return validateForm()" > ... </FORM>
Interact with form
Radio buttons, checkboxes, listboxes and edit fields can handle onchange event. Edit box will trigger that event only when it loses focus.
HTML <select name="comboOptions" onchange="alert('You selected ' + this.value)"> ... </select>
3. Setting visibility of elements
JavaScript if (shouldBeHidden) { document.getElementById("myId").style.display = "none"; }else { document.getElementById("myId").style.display = "block"; }
4. AJAX
JavaScript function onClick() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { // IE 5,6 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("lblHello").innerText = xmlhttp.responseText; } } xmlhttp.open("GET", "http://localhost/ajax.txt", true); xmlhttp.send(); }
HTML <div id="lblHello">will be updated with AJAX</div> <input id="btnGet" type="button" value="Make AJAX call" onclick="onClick()"/>
Code example:Ajax
(requires http://localhost/ajax.txt)JQuery abstracts browsers differences and simplifies the code.
5. Object
JavaScript var person = { firstName: "John", lastName: "First" }; alert(person.firstName);or using function
JavaScript function Person() { this.firstName = "John"; this.lastName = "First"; } var p = new Person();
6. Knockout data binding
The code below implements one way binding for firstName and two way binding (changes made by user are immediately reflected to the underlying variable) for last name.
HTML <html> <head> <script type='text/javascript' src="knockout-3.1.0.debug.js"></script> </head> <body> First name: <span data-bind="text: firstName"></span><br/> Last name: <input data-bind="value: lastName" /><br/> <button onclick="alert(_viewModel.firstName)">Show First name</button><br/> <button onclick="alert(_viewModel.lastName())">Show Last name</button> <script> var _viewModel = { firstName: "John", lastName: ko.observable("First") }; ko.applyBindings(_viewModel); </script> </body> </html>