Tuesday, March 27, 2012

How to clear form using jquery

<!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>
    <title></title> 
                                                                         // add your reference jquery  file below
    <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
                                                       
    <script type="text/javascript">
        (function ($) {
            $.fn.clearForm = function () {
                return this.each(function () {
                    var obj = $(this);
                    obj.find('[type="text"],[type="password"],[type="hidden"],textarea').val('');
                    obj.find('[type="checkbox"],[type="radio"]').attr('checked', false);
                    obj.find('select').val(-1);
                });
            };
        })(jQuery);       

    </script>

    <script type="text/javascript">
        $(document).ready(function () {
            $('#btnClearForm').click(function () {
                $('#form1').clearForm();
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    Email
                </td>
                <td>
                    <input type="text" id="tbEmail" />
                </td>
            </tr>
            <tr>
                <td>
                    Password
                </td>
                <td>
                    <input type="password" id="tbPassword" />
                </td>
            </tr>
            <tr>
                <td>
                    HiddenValue
                </td>
                <td>
                    <input type="hidden" id="hdnHiddenValue" />
                </td>
            </tr>
            <tr>
                <td>
                    Details
                </td>
                <td>
                    <textarea style="width: 300px; height: 300px;" id="tbDetails"></textarea>
                </td>
            </tr>
            <tr>
                <td>
                    Sex
                </td>
                <td>
                    <input type="radio" value="Male" name="rblSex" />Male<br />
                    <input type="radio" value="Female" name="rblSex" />Female
                </td>
            </tr>
            <tr>
                <td>
                    Property
                </td>
                <td>
                    <input type="checkbox" value="Car" name="cbProperty" />Car<br />
                    <input type="checkbox" value="House" name="cbProperty" />House<br />
                    <input type="checkbox" value="Van" name="cbProperty" />Van<br />
                </td>
            </tr>
            <tr>
                <td>
                    Status
                </td>
                <td>
                    <select id="ddlStatus">
                        <option>Select</option>
                        <option value="single">single</option>
                        <option value="married">married</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="button" id="btnClearForm" value="Clear Form" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

No comments:

Post a Comment