Java Script Examples

1. Function example

Javascript:

[script>
    function buttonClick() {
        document.getElementById('label1').innerHTML = "The button was clicked";
    }
[/script>
         

HTML:

[button id="button1" type="button" onclick="buttonClick()" >Click Me[/button>
[label id="label1"  /> 
         

Function execution:

2. Input Function example

Javascript:

[script>
    function squaredFunction() {
        var num = document.getElementById('num').value;
        var sq = num * num;
        document.getElementById('label2').innerHTML = "The input squared is " + sq;
    }
[/script>
         

HTML:

Number: [input type="number" value="3" id="num" name="num" />
[button id="button2" type="button" onclick="squaredFunction()" >Square the input[/button>
[label id="label2"  /> 
         

Return Function execution:

Number:

3. Return Function example

Javascript:

    function returnFunction(x) {
        var sq = x * x;
        return sq;
    }
         

HTML:

Number: [input type="number" value="2" id="num3" name="num3" />
[button id="button3" type="button" onclick=" var num = document.getElementById('num3').value; 
var sq = returnFunction(num); 
document.getElementById('label3').innerHTML = 'The input squared is ' + sq;" >Show the returned input squared[/button>
[label id="label3"  /> 
         

Return Function execution:

Number:

4. Verify number Function example

Javascript:

         function vernumFunction(x) {
           return !isNaN(parseFloat(x)) && isFinite(x);
         }
         

HTML:

Number: [input type="text" value="4" id="num4" name="num4" />
[button id="button4" type="button" onclick=" var num = document.getElementById('num4').value; 
document.getElementById('label4').innerHTML = 'The input is a number? ' + IsNumeric(num);" >Check input is numeric[/button>
[label id="label4"  /> 
         

Verify Number Function execution:

Number:

5. Loop thru an Array example

Javascript:

         function looparrayFunction() {
            var inpt = document.getElementById('num5').value;
            var nums = inpt.split(','); 
            var outpt = "";
            // Do NOT use 'in' for arrays
            for (var x = 0; x < nums.length;  x++) {
                outpt = outpt + nums[x] + "
"; } document.getElementById('label5').innerHTML = outpt; }

HTML:

         Number: [input type="text" value="5,2,3,1,4" id="num5" name="num5" />
         [button id="button5" type="button" onclick="looparrayFunction();" >Loop thru an array[/[button>
         [label id="label5"  />    
         

Loop thru an array execution:

Number:

6. Loop thru a Collection example

Javascript:

         function loopobjectFunction() {
            var inpt = document.getElementById('num6').value;
            var nums = inpt.split(',');
            var outpt = "";
            for (var x in nums) {
                outpt = outpt + nums[x] + "
"; } document.getElementById('label6').innerHTML = outpt; }

HTML:

         Number: [input type="text" value="a,b,c" id="num6" name="num6" />
         [button id="button6" type="button" onclick="loopobjectFunction();" >Loop thru a collection[/[button>
         [label id="label6"  />    
         

Loop thru a collection execution:

Number:

7. Create, Maintain, and List an App example

Javascript:

         
         

HTML:

           
         

Create/Maintain an App execution:



Id:
Name:
Address:






8. Maintain a Table example

Maintain a Table execution: