Tuesday, 8 April 2014

How to increase and decrease font size of text content using JQuery?

Description:

Welcome to Jqueryjohn.com.

In this article, I will explain how to increase and decrease font size of the page using Jquery.  

Lot of websites provides users with an option to increase or decrease the font size of the page for better reading experience.

In the below program, I have used small JavaScript code to change the font size of the page at client side.

When the page loads, font size is set to 14px by default. When the user clicks the button, font size is increased or decreased by 2 Px.

For live demo, click here.

Example program:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var defSize = 14;
            var size = defSize + "px";
            $(".divOne").css("font-size", size);
            $('#btnZoomIn').click(function () {
                defSize = defSize + 2;
                var size = defSize + "px";
                $(".divOne").css("font-size", size);
                return false;
            });
            $('#btnZoomOut').click(function () {
                defSize = defSize - 2;
                var size = defSize + "px";
                $(".divOne").css("font-size", size);
                return false;
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div class="divOne">
            Welcome to Jqueryjohn.com. This is Technical blog related to Scripting Languages.
            Jqueryjohn.com offers jquery related articles with code snippets. Jqueryjohn.com
            also has jquery tutorials with examples. It is easy to learn for beginners. Donec.
        </div>
        <asp:Button ID="btnZoomIn" runat="server" Text="Zoom In" />
        <asp:Button ID="btnZoomOut" runat="server" Text="Zoom Out" />
    </div>
    </form>
</body>
</html>

Increase or decrease font size of the page dynamically, Adjusting font size with jquery, resize font size using jquery , change font size using jquery, dynamically change font size of the webpage using jquery

How to dynamically display sum of all textbox values to another textbox using JQuery?

Description:

Welcome to Jqueryjohn.com.

In this article, I will explain how to display sum of all textbox values using Jquery.  

One of my readers asked me one question that how to sum of all textbox values while user typing on client side. So I decided to write post for this.

Whenever user change the value of the textbox, sum of the textbox values are calculated and displayed on other textbox.

For live demo, click here.

Example program:

<html>
<head>
    <title>Sum Textbox Values using JQuery</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('.addValue').each(function () {
                $(this).keyup(function () {
                    SumValues();
                });
            });
        });
        function SumValues() {
            var sum = 0;
            $('.addValue').each(function () {
                if (!isNaN(this.value) && this.value.length != 0) {
                    sum += parseFloat(this.value);
                    $(this).css("background-color", "#FEFFB0");
                }
                else if (this.value.length != 0) {
                    $(this).css("background-color", "red");
                }
            });
            $('#totalValue').val(sum);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <table>
        <tr>
            <td>
                Value 1 :
            </td>
            <td>
                <input id="Text1" type="text" class="addValue" />
            </td>
        </tr>
        <tr>
            <td>
                Value 2 :
            </td>
            <td>
                <input id="Text2" type="text" class="addValue" />
            </td>
        </tr>
        <tr>
            <td>
                Sum Value:
            </td>
            <td>
                <input id="totalValue" type="text" disabled="disabled" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>


Display sum of all textbox values to another textbox using JQuery, sum of all textbox values while typing, sum values in textbox while typing

Saturday, 5 April 2014

How to add Google maps to your website using JQuery?

Description:

Welcome to Jqueryjohn.com.

In this article, I will explain how to add Google Maps on website using Jquery.  

Google maps offer more power to web designers. Google Maps can able to provide street maps and route planner for travelling. It is easy to understand maps and directions and it can bring more customers through your door.

Steps for adding Google Maps:

1. Add a following file from Google CDN.
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>

2. Download Google Maps Plugin.
You can download from https://github.com/hpneo/gmaps (or) http://hpneo.github.io/gmaps/documentation.html

2. Extract it and include the files gmaps.js and latest JQuery version.

3. Once the document is ready, initialize the Google Map widget to the div element.

4. We can get latitude and longitude by the following steps.

Open Google Maps website.

Use the zoom controls on the map to find the place which you want to set by default.

Now, right click on the place, select what’s here? Option and you will get two values at the top of the page.

5. We can also add markers to highlight the place on the map.

In the below program, I added Google maps on the particular div and added marker for highlighting the particular position.

Example program:

<html>
<head>
    <title>Basic</title>
    <script src="jquery-1.10.2.js" type="text/javascript"></script>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true">
    </script>
    <script src="gmaps.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var mapArea = new GMaps({
                zoom: 9, //Set Zoom Level
                el: '#divMap',
                lat: 8.565081,
                lng: 77.968092,
                zoomControl: true,
                zoomControlOpt: {
                    style: 'LARGE',
                    position: 'RIGHT_CENTER'
                },
                panControl: true, // used for navigating left,right,top and bottom
                streetViewControl: true,
                mapTypeControl: true, //used for displaying map/satellite mode
                overviewMapControl: false
            });
            mapArea.addMarker({
                lat: 8.565081,
                lng: 77.968092,
                title: 'Nazareth',
                click: function (e) {
                    alert('Nazareth');
                }
            })
        });
    </script>
    <style type="text/css">
        #divMap
        {
            display: block;
            width: 95%;
            height: 350px;
            margin: 0 auto;
            box-shadow: 0px 5px 20px #ccc;
        }
    </style>
</head>
<body>
    <div>
        <div id="divMap">
        </div>
    </div>
</body>
</html>


Adding Google Maps to your site, Adding custom Google maps to your website using jQuery, Embed Google map in your webpage

How to create fixed header when scrolling the page using JQuery?

Description:

Welcome to Jqueryjohn.com.

In this article, I will explain how to create fixed header while scrolling the page using Jquery.  

While doing scrolling on the webpage, the header should remain top of the screen. To implement this functionality, I have used simple JavaScript code.

Create a header div which we want to freeze at the top.

In the below code, I have added event listener to scroll event. This event is called each time when scroll event is fired.

While page is scrolling down, set the top attribute to 0px and make the header position fixed.

For live demo, click here.


Example program:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-1.9.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var fixHeader = $('#fixedHeader').offset().top;
            $(window).scroll(function () {
                if ($(window).scrollTop() > fixHeader) {
                    $('#fixedHeader').css({ position: 'fixed', top: '0px' });
                } else {
                    $('#fixedHeader').css({ position: 'static', top: '0px' });
                }
            });
        });
    </script>
    <style type="text/css">
        #fixedHeader
        {
            background-color: Gray;
            height: 60px;
            width: 990px;
        }
        #fixedHeader ul
        {
            margin: 0 10px;
            padding: 10px 40px 15px;
            box-shadow: 0 12px 5px -6px rgba(0, 0, 0, 0.5);
        }
        #fixedHeader ul li
        {
            display: inline;
            font-weight: bold;
            margin: 0 20px;
        }
    </style>
</head>
<body>
    <div id="fixedHeader">
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Articles</a></li>
            <li><a href="#">Tutorials</a></li>
            <li><a href="#">PlugIns</a></li>
        </ul>
    </div>
    <div id="content">
        <h3>
            Add your page content Here</h3>
        <p>
            This is the sample content for the page.
        </p>
    </div>
</body>
</html>


Create fixed header while scrolling using jquery, fixed header using jquery, facebook like header with fixed position, jquery scrollable div with fixed header

Friday, 4 April 2014

How to create simple Gridview using JQuery and Ajax?


Description:

Welcome to Jqueryjohn.com.

In this article, I will explain how to create simple JQuery Gridview using Ajax.

One of my friends asked me to add, edit and delete a record to html table like Gridview on client side without postback. I have chosen JQuery for this.

Using JQuery Gridview, user can able to Add, Edit and Delete a record without refreshing the page.

For displaying the records like Gridview, i am getting the records as Json object from the server using Ajax and binding with the html table.

Ajax used to connect with the server side without reloading the page.

If the user wants to add new row, it opens new dialog window with the input fields. Just add the values and save it. Added data will append with the html table without refreshing the page.

If the user wants to update a particular record, click the update image on the row. It opens dialog window, modify the values and save it.

While calling AJAX method through JQuery, the method should be static and it should be web method.

Advantage of having JQuery Gridview is, there is no postback for every operation. It increases performance also.

Example program:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script src="jquery-1.10.2.js" type="text/javascript"></script>
    <script src="jquery-ui-1.10.3.custom.js" type="text/javascript"></script>
    <link href="jquery-ui-1.10.3.custom.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        $(document).ready(function () {
            LoadPage();
            $("#PopupContent").dialog({
                title: "PopUp Title",
                autoOpen: false,
                height: 200,
                width: 300,
                modal: true
            });
            $("#btnSave").click(function () {
                DoOperation();
                return false;
            });
            $("#btnCancel").click(function () {
                DefaultMode();
                return false;
            });
        });
    </script>
    <script type="text/javascript">
        function DefaultMode() {
            $("#PopupContent").dialog("close");
        }
        function AddUpdateMode() {
            $("#PopupContent").dialog("open");
        }
        function LoadPage() {
            $.ajax({
                type: "POST",
                url: "AJAXTABLE.aspx/GetRecords",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function successcall(response) {
                    $("#tblGrid").empty();
                    $("#tblGrid").prepend("<tr>" + "<th>" + "" + " </th>" + "<th>" + "" + " </th>" + "<th>" + "" + "</th>" + "<th>" + "" + "</th>" + "<th>" + "<iimg src='add.png' onclick=\"AddMode() \"/>" + " </th>" + "</tr>");
                    $("#tblGrid").append("<tr>" + "<th>" + "ID" + " </th>" + "<th>" + "Name" + " </th>" + "<th>" + "Age" + "</th>" + "<th>" + "Update" + " </th>" + "<th>" + "Delete" + "</th>" + "</tr>");
                    for (var i = 0; i < response.d.length; i++) {
                        $("#tblGrid").append("<tr>" + "<td>" + response.d[i].Id + " </td>" + "<td>" + response.d[i].Name + " </td>" + "<td>" + response.d[i].Age + "</td>" + "<td>" + "<iimg src='update.png' onclick=\"UpdateMode(" + response.d[i].Id + ",'" + response.d[i].Name + "'," + response.d[i].Age + ")\" />" + "</td>" + "<td>" + "<iimg src='delete.png' onclick=\"DeleteMode(" + response.d[i].Id + ",'" + response.d[i].Name + "'," + response.d[i].Age + ")\" />" + "</td>" + "</tr>"); //replace iimg to img
                    }
                },
                error: function () {
                    alert("Error");
                }
            });
            return false;
        }
        function DoOperation() {
            var hidvalue = $("#hdnMode").val();
            if (hidvalue == "Add") {
                var pID = "0";
                var name = $('#txtName').val();
                var age = $('#txtAge').val();
            }
            else if (hidvalue == "Update") {
                var name = $('#txtName').val();
                var age = $('#txtAge').val();
                var pID = $('#hdnPkid').val();
            }
            else if (hidvalue == "Delete") {
                var name = "";
                var age = "";
                var pID = $('#hdnPkid').val();
            }
            $.ajax({
                type: "POST",
                url: "AJAXTABLE.aspx/DoOperation",
                data: "{'Id':'" + pID + "','Name': '" + name + "','Age':'" + age + "','HidMode':'" + hidvalue + "' }",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    if (response.d == true) {
                        $('#hdnPkid').val("");
                        DefaultMode();
                        LoadPage();
                    }
                    else {
                        alert("Failure!!!");
                    }
                }
            });
            return false;
        }
        function AddMode() {
            AddUpdateMode();
            $('#txtName').val("");
            $('#txtAge').val("");
            $("#hdnMode").val("Add");
        }
        function UpdateMode(Id, Name, Age) {
            AddUpdateMode();
            $("#hdnMode").val("Update");
            $('#txtName').val(Name);
            $('#txtAge').val(Age);
            $('#hdnPkid').val(Id);
        }
        function DeleteMode(Id, Name, Age) {
            $("#hdnMode").val("Delete");
            $('#hdnPkid').val(Id);
            if (confirm("Do you want to delete this record")) {
                DoOperation();
            }
        }
    </script>
    <style type="text/css">
        #ParentContent table
        {
            width: 300px;
            border: 1px;
        }
        #ParentContent table td
        {
            width: 60px;
            color: Red;
            text-align: center;
        }
        #ParentContent table th
        {
            width: 60px;
            color: Green;
            text-align: center;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="ParentContent">
            <table id="tblGrid" border="1px">
            </table>
        </div>
        <div id="PopupContent">
            <table>
                <tr>
                    <td>
                        <asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
                    </td>
                    <td style="width: 50%">
                        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lblAge" runat="server" Text="Age"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="btnSave" runat="server" Text="Save" />
                    </td>
                    <td>
                        <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
                        <input id="hdnMode" type="hidden" name="hdnMode" runat="server" />
                        <input id="hdnPkid" type="hidden" name="hdnPkid" runat="server" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    </form>
</body>
</html>

Code behind:

namespace JqueryJohn_sample
{
    public partial class AJAXTABLE : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [System.Web.Services.WebMethod]
        public static clsTSample[] GetRecords()
        {
            List<clsTSample> obj = new List<clsTSample>();
            DataTable dt = new DataTable();
            SqlConnection con = new SqlConnection("Data Source=---;Initial Catalog=dbname;User ID=---;Password=---;");
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from TSample", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            con.Close();
            clsTSample ClsObj = null;
            foreach (DataRow tblerow in dt.Rows)
            {
                ClsObj = new clsTSample();
                ClsObj.Id = Convert.ToInt32(tblerow["Id"]);
                ClsObj.Name = tblerow["Name"].ToString();
                ClsObj.Age = Convert.ToInt32(tblerow["Age"]);
                obj.Add(ClsObj);
            }
            return obj.ToArray();
        }

        [System.Web.Services.WebMethod]
        public static bool DoOperation(string Id, string Name, string Age, string HidMode)
        {
            try
            {
                SqlConnection con = new SqlConnection("Data Source=----;Initial Catalog=dbname;User ID=--;Password=---;");
                con.Open();
                if (HidMode == "Add")
                {
                    SqlCommand cmd = new SqlCommand("insert into TSample (Name, Age) values('" + Name + "','" + Age + "')", con);
                    cmd.ExecuteNonQuery();
                }
                else if (HidMode == "Update")
                {
                    int SId = Convert.ToInt32(Id);
                    SqlCommand cmd = new SqlCommand("update TSample set Name='" + Name + "', Age='" + Age + "' where id='" + SId + "'", con);
                    cmd.ExecuteNonQuery();
                }
                else if (HidMode == "Delete")
                {
                    int SId = Convert.ToInt32(Id);
                    SqlCommand cmd = new SqlCommand("delete from TSample where id='" + SId + "'", con);
                    cmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }


    }
    public class clsTSample
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

    }

}

The above code tested successfully and it works fine.


Create gridview dynamically using jquery,  Add, edit and delete data on gridview using jquery, create simple gridview using jquery, bind data to table or gridview using jquery,  perform operation on gridview without postback using jquery.