Tuesday, March 27, 2012

How to clear form for particular filed only using jquery

 // Here except Email and Password field alone will not be cleared remaing fileds will be cleared.
 // class="ClearForm"  because not exist.

// class="ClearForm Corner" this is multiple class style. You can add more than one class style to 
// htmal tag.

<!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>
    <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);
                    var objClass = '.ClearForm';

                    obj.find('[type="text"]' + objClass + ',[type="password"]' + objClass + ',[type="hidden"]' + objClass + ',textarea' + objClass).val('');
                    obj.find('[type="checkbox"]' + objClass + ',[type="radio"]' + objClass).attr('checked', false);
                    obj.find('select' + objClass).val(-1);
                });
            };
        })(jQuery);        

    </script>
    <script type="text/javascript">
//  Here Email and Password field alone will not be cleared.  class="ClearForm"  because not exist.
        $(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" class=" Corner" />
                </td>
            </tr>
            <tr>
                <td>
                    Password
                </td>
                <td>
                    <input type="password" id="tbPassword" class="Corner" />
                </td>
            </tr>
            <tr>
                <td>
                    HiddenValue
                </td>
                <td>
                    <input type="hidden" id="hdnHiddenValue" class="ClearForm Corner" />
                </td>
            </tr>
            <tr>
                <td>
                    Details
                </td>
                <td>
                    <textarea style="width: 300px; height: 300px;" id="tbDetails" class="ClearForm Corner"></textarea>
                </td>
            </tr>
            <tr>
                <td>
                    Sex
                </td>
                <td>
                    <input type="radio" value="Male" name="rblSex" class="ClearForm RedColor" />Male<br />
                    <input type="radio" value="Female" name="rblSex" class="ClearForm RedColor" />Female
                </td>
            </tr>
            <tr>
                <td>
                    Property
                </td>
                <td>
                    <input type="checkbox" value="Car" name="cbProperty" class="ClearForm RedColor" />Car<br />
                    <input type="checkbox" value="House" name="cbProperty" class="ClearForm RedColor" />House<br />
                    <input type="checkbox" value="Van" name="cbProperty" class="ClearForm RedColor" />Van<br />
                </td>
            </tr>
            <tr>
                <td>
                    Status
                </td>
                <td>
                    <select id="ddlStatus" class="ClearForm RedColor">
                        <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>

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>