Menu ▼


<< < 1 2 3 4 > >>

Using jQuery animate you can animate the controls. In this example we will see how to animate the control width using jQuery animate and toggle.

Example:
Click on menu icon to animate


jQuery code to slide/animate menu bar

    <script language="javascript">
        $(document).ready(function () {
            $(".menuicon").on("click", function () {
                $(".menu").animate({ width: 'toggle' }, "slow");
            });
        });
    </script>

Source code:

<html>
<head>
    <style>
        .menu {
            float: left;
            display: none;
            padding: 0px 5px;
        }

            .menu ul {
                list-style-type: none;
                margin: 0;
                padding: 0;
            }

                .menu ul li {
                    margin: 0;
                    padding: 0;
                }

                    .menu ul li a {
                        text-decoration: none;
                        color: #292323;
                        background: #ccc;
                        display: block;
                        padding: 5px 30px 5px 5px;
                        border-bottom: solid 1px #fff;
                        height: 20px;
                        white-space: nowrap;
                    }

        .menuicon {
            border: solid 1px #000;
            border-radius: 50%;
            padding: 5px 8px;
            float: left;
            cursor: pointer;
        }

        .menuiconbar {
            width: 25px;
            height: 5px;
            background-color: black;
            margin: 3px 0;
        }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script language="javascript">
        $(document).ready(function () {
            $(".menuicon").on("click", function () {
                $(".menu").animate({ width: 'toggle' }, "slow");
            });
        });
    </script>
</head>
<body>
    <div>
        <div class="menu">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">Blogs</a></li>
                <li><a href="#">Tutorials</a></li>
                <li><a href="#">Forums</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </div>
        <div class="menuicon">
            <div class="menuiconbar"></div>
            <div class="menuiconbar"></div>
            <div class="menuiconbar"></div>
        </div>
    </div>
    <br clear="all" />
</body>
</html>

Read More...
Submited By : mrkraju On 06 Oct 2016

This example shows you how to blink div background using jQuery toggleClass and javascript setInterval.

jQuery toggleClass() example!



Source code:
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <style type="text/css">
        .divmsg {
            background: green;
            color: #fff;
            display: inline-block;
            margin: 20px;
            padding: 20px;
            border-radius:5px;
        }

        .divred {
            background: red !important;
        }

        #btntoggle, #btnstoptoggle {
            margin: 10px 20px;
            padding:10px;
        }
    </style>
</head>
<body>
    <div class="divmsg">
        jQuery toggleClass() example!
    </div><br />
    <input type="button" id="btntoggle" value="Blink background using toogle" /><br />
    <input type="button" id="btnstoptoggle" value="Stop blinking" />
    <script src="http://code.jquery.com/jquery-1.7.2.min.js" language="javascript"></script>
    <script language="javascript">
        $(document).ready(function () {
            var blink = null;
            $("#btntoggle").on("click", function () {
                if (blink == null)
                    blink = setInterval(blinkMessage, 500);
            });
            $("#btnstoptoggle").on("click", function () {
                if (blink != null) {
                    clearInterval(blink);
                    blink = null;
                    $(".divmsg").removeClass("divred");
                }
            });
        });

        function blinkMessage() {
            $(".divmsg").toggleClass("divred");
        }
    </script>
</body>
</html>

Read More...
Submited By : mrkraju On 25 Sep 2016

This example shows you how to disable or enable all controls/elements in a div using jquery. To disable all controls you need to set the disabled attribute value to disabled using jquery attr() method. to re-enable all the controls you need to remove the disabled attribute by using jquery removeAttr() method;

Syntax

//To disable
$("#divContainer").find("input, button, submit, textarea, select").attr("disabled", "disabled");

//To re-enable
$("#divContainer").find("input, button, submit, textarea, select").removeAttr("disabled");

Note
Anchor(hyperlink) elements don't support disabled attribute so we have to implement on click event as e.preventDefault();. To re-enable hyperlink we have to unbind the click event.
Syntax

//To disable
$("#divContainer").find("a").addClass("disablehyper").click(function (e) {                
    e.preventDefault();
});

//To re-enable
$("#divContainer").find("a").removeClass("disablehyper").unbind("click");

Example
  

UserName

Password

Rember me


Home Page

Source code

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style>
        .disablehyper
        {
            color: #ccc;
            cursor: text;
        }
        .disablehyper:hover
        {
            color: #ccc;
            cursor: text;
        }
    </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script language="javascript">
        $(document).ready(function () {
            $("#btnDisable").on("click", function () {
                $("#divContainer").find("input, button, submit, textarea, select").attr("disabled", "disabled");
                $("#divContainer").find("a").addClass("disablehyper").click(function (e) {
                    e.preventDefault();
                });
            });
            $("#btnEnable").on("click", function () {
                $("#divContainer").find("input, button, submit, textarea, select").removeAttr("disabled");
                $("#divContainer").find("a").removeClass("disablehyper").unbind("click");
            });
        });
    </script>
</head>
<body>
    <input type="button" id="btnDisable" value="Disable Controls" />
    <input type="button" id="btnEnable" value="Enable Controls" />
    <div id="divContainer" style="broder: solid 1px #ccc; padding: 10px;">
        UserName<br />
        <input type="text" id="txtUserName" />
        <br />
        Password<br />
        <input type="password" id="txtPassword" />
        <br />
        <input type="checkbox" id="chbRember" />Rember me
        <br />
        <input type="button" id="btnLogin" value="Login" />
        <br />
        <br />
        <a href="/home.aspx" target="_blank">Home Page</a>
    </div>
</body>
</html>

Read More...
Submited By : mrkraju On 09 Dec 2015

This example shows you how to set the iframe source(src) using jquery and how to change the iframe source dynamically. In this example a dropdown contains list of page urls, when you select the page url from the dropdown it will be loaded in the iframe by assigning the src using jquery.

Syntax

$("#ifrm").attr("src", pageurl);

Example
Page Source


Source code

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script language="javascript">
        $(document).ready(function () {
            $("#ddlsource").on("change", function () {
                var pageurl = $(this).val();
                $("#ifrm").attr("src", pageurl);
            });
        });
    </script>
</head>
<body>
    Page Source<br />
    <select id="ddlsource">
        <option value="">Select</option>
        <option value="http://dotnetlearners.com/tutorial/angularjs/17/angularjs-tutorial-introuduction-to-angularjs">
            AngularJS Tutorial</option>
        <option value="http://dotnetlearners.com/stats.aspx">Stats</option>
        <option value="http://dotnetlearners.com/home.aspx">Home</option>
    </select>
    <br />
    <br />
    <iframe id="ifrm" height="500" width="500" scrolling="auto"></iframe>
</body>
</html>

Read More...
Submited By : mrkraju On 08 Dec 2015

This example shows you how to show the html as text in div using jquery with out rendering. By using jquery text() function we can assign the html code to div and additionally we have to replace the space with &nbsp; and new line(\n) with html <br/>

jQuery code

<script language="javascript">
    $(document).ready(function () {
        $("#txtInput").on("keyup", function () {
            $("#divOutput").text($("#txtInput").val());
            $("#divOutput").html($("#divOutput").html().replace(/\n/g, '<br/>').replace(/ /g, '&nbsp;'));
        });
    });
</script>

Example

Enter or copy paste html code in the below textbox


Output

Source code

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script language="javascript">
        $(document).ready(function () {
            $("#txtInput").on("keyup", function () {
                $("#divOutput").text($("#txtInput").val());
                $("#divOutput").html($("#divOutput").html().replace(/\n/g, '<br/>').replace(/ /g, '&nbsp;'));
            });
        });
    </script>
</head>
<body>
    Input<br />
    <textarea id="txtInput" rows="20" style="width: 100%;"></textarea>
    <br />
    <br />
    <div id="divOutput" style="border: solid 1px #ccc; padding: 20px; background: #ffe;">
    </div>
</body>
</html>

Read More...
Submited By : mrkraju On 05 Dec 2015

jQuery.merge() function is used to merge the content of two arrays. merge function will merge the content of two arrays into the first array.

Example 1
Below example will merge the first array elements and second array elements into first array.

Two arrays:

var firstary = ["one", "two", "three"];
var secondary = ["four", "five", "six"];

Merge Two arrays
$.merge(firstary, secondary);

Output
firstary -> ["one", "two", "three", "four", "five", "six"];
secondary -> ["four", "five", "six"];

Example 2
Below example will merge the first array elements and second array elements into new array and keeps the first and second array elements as it is.

Two arrays:

var firstary = ["one", "two", "three"];
var secondary = ["four", "five", "six"];

Merge Two arrays into new array
var thirdary = $.merge($.merge([], firstary), secondary);

Output
firstary -> ["one", "two", "three"];
secondary -> ["four", "five", "six"];
thirdary -> ["one", "two", "three", "four", "five", "six"];

Read More...
Submited By : mrkraju On 21 Nov 2015

To trim string value using jquery simple use $.trim()

Below example will show you how to trim a string.

var str = "    Hello    ";
str = $.trim(str);

Below example will trim the textbox value when click on Trim button.

Input :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Login Page</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
        function trimtext() {
            var str = $("#txtInput").val();
            str = $.trim(str);
            $("#txtInput").val(str);
        }
    </script>
</head>
<body>
    <div>
        Input :
        <input type="text" id="txtInput" value="        Hello       " />
        <input type="button" value="Trim" onclick="trimtext();" />
    </div>
</body>
</html>

Read More...
Submited By : mrkraju On 19 Nov 2015

This example shows you how to clear all textbox values in a page using jQuery. jQuery selector $("input:text") will select all the textboxes in the current document and $("input:text").val("") will clear all the textbox value.

Syntax

$("input:text").val("");

Text1 :

Text2 :

Text3 :

Source Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Login Page</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
        function cleartextboxes() {
            $("input:text").val("");
        }
    </script>
</head>
<body>
    <div>
        Text1 :
        <input type="text" value="Hello" />
        <br />
        <br />
        Text2 :
        <input type="text" value="Example" />
        <br />
        <br />
        Text3 :
        <input type="text" value="jQuery examples" />
        <br />
        <br />
        <input type="button" value="Clear" onclick="cleartextboxes();" />
    </div>
</body>
</html>

Read More...
Submited By : mrkraju On 18 Nov 2015

This example shows you how to find the fileupload control selected file size using jquery.

Example:


Code:
$(document).ready(function () {
    $('#fupexample').bind('change', function () {
        var size = this.files[0].size;
        var str = "File Size:\n" + size + " Bytes";
        str += "\n" + (size / 1024).toFixed(2) + " KB";
        str += "\n" + ((size / 1024) / 1024).toFixed(2) + " MB";
        alert(str);
    });
});

Source Code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    <script language="javascript">
        $(document).ready(function () {
            $('#fupexample').bind('change', function () {
                var size = this.files[0].size;
                var str = "File Size:\n" + size + " Bytes";
                str += "\n" + (size / 1024).toFixed(2) + " KB";
                str += "\n" + ((size / 1024) / 1024).toFixed(2) + " MB";
                alert(str);
            });
        });
    </script>
</head>
<body>
    <input type="file" id="fupexample" name="fupexample" style="width: 200px;" />
</body>
</html>
Read More...
Submited By : mrkraju On 02 May 2015

This example shows you how to check the file extension from file upload control selected value or from a given file name.


Example:
  

Code:
function CheckFileExtension() {
    var ext = $("#fupSample").val().split('.').pop();
    alert(ext);
};

Source Code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    <script language="javascript">
        function CheckFileExtension() {
            var ext = $("#fupSample").val().split('.').pop();
            alert(ext);
        };
    </script>
</head>
<body>
    <div>
        <input type="file" id="fupSample" style="width: 200px;" />&nbsp;&nbsp;
        <input type="button" value="Check Extension" onclick="CheckFileExtension();" />
    </div>
</body>
</html>
Read More...
Submited By : mrkraju On 28 Apr 2015
<< < 1 2 3 4 > >>




Search dotnetlearners.com