Friday, 24 October 2014

Detecting date selection in the JQuery datepicker control

The following function will attached the callback function to the JQuery datepicker control whenever the user choose a date from the popup:

function dt_attach_value_change_event(ctl_id, callback) {
    $(ctl_id).datepicker("option", "onSelect", callback);
}

Sunday, 24 November 2013

Close popup div when clicked outside


Below is the script that solves the mentioned problem. I have not personally tested the code. You might want to try it out yourself.



$(function(){
   $("#btnFieldWorkers").click(function(e){       
       e.stopPropagation();
       $("#div_fieldWorkers").show();
       $("body").click(function(e){
           if(e.target.id != "div_fieldWorkers")
           {
               $("#div_fieldWorkers").hide();
               $("body").unbind("click");
           }
       });
   });

});
 
 
Reference: 

http://stackoverflow.com/questions/19092433/close-popup-div-when-clicked-outside 

Tuesday, 4 June 2013

RGB-to-Hex Conversion

This is the RGB-to-Hex Conversion Javascript:

http://www.javascripter.net/faq/rgbtohex.htm

Tuesday, 19 March 2013

Sorting data using JavaScript

Sorting in Javascript is quite easy.

For example, I have an array call "my_object_array" where "my_object" is an object that contains "customer_code" and "customer_name" properties. To sort the data by customer_code, just run the following code:

var f = 'customer_code';

my_object_array.sort(function (a, b) {
            return a[f] - b[f];
        });

To sort the data in descending order, you just need to swap the "a" and "b".

my_object_array.sort(function (a, b) {
            return b[f] - a[f];
        });

Cool huh?

Friday, 1 March 2013

Submitting data in JSON format to the website

If your web page contains quite a number of fields to be posted to the web server, you may submit the data in JSON format (through JQuery). In this way, you may get rid of writing lots of query parameter in the URL.

Below is the sample code posted by someone.

Sample code
http://stackoverflow.com/questions/5570747/jquery-posting-json

JSON Object
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify

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();

We are moving

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