Monday, 21 January 2013

Javascript parameter is optional

In Javascript, the parameter in the function is optional. If the caller did not pass in the parameter value (as shown in test1 function), then, the value of "a" is "undefined". So, each time before you are using the incoming parameter value, make sure that you check it before using it.


You may copy the following tags into any html file and then debug it in Visual Studio.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        function check(a) {
            var i = 0;
            if (a) {
                i = a;
            }
            else {
                i = -1;
            }
        }
        function test1() {
            check();
        }
        function test2() {
            check(12345);
        }
        function test3() {
            check(null);
        }
    </script>
</head>
<body>
    <input type="button" name="name" value="test1" onclick="return test1();" />
    <input type="button" name="name" value="test2" onclick="return test2();" />
    <input type="button" name="name" value="test3" onclick="return test3();" />
</body>
</html>

Friday, 11 January 2013

Clearing the contents in an element

Execute the following using JQuery, it will clear all the contents (including tags) within "div1":

   $('#div1').remove().end();

Wednesday, 2 January 2013

Get the input by ID

Let's say you have 3 buttons which starts with "f1" and below is the HTML tags:

        <input type="button" id="f1.1" value="test1" />
        <input type="button" id="f1.2" value="test2" />
        <input type="button" id="f1.3" value="test3" />

When you run the following JQuery code, you will get different result:

            var v1 = $('#f1.1');              => this returns zero match.
            var v2 = $('input[id^="f1"]');  => this returns 3 matches.
            var v3 = $('input[id="f1.2"]'); => this returns 1 match (i.e., the second button).

If you are using "#" in the selector, the result is either 0 or 1 (it's always stop after it found the first one).

For more details, check out the following reference:

    http://api.jquery.com/category/selectors/

We are moving

We are moving this blog to our new blog site: https://ciysys.com/blog/nodejs.htm