how to clear an input after submit in jquery?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fris
    I have to go potty
    • Aug 2002
    • 55721

    #1

    how to clear an input after submit in jquery?

    here is the code i have (it does live search)

    Code:
    jQuery(document).ready(function ($) {
        var contentCache, searched = false;
        jQuery('#searchform').submit(function () {
            var s = jQuery(this).find('#s').val();
            if (s.length == 0) {
                return;
            }
            var submit = $('#searchsubmit');
            submit.attr('disabled', false);
            var url = jQuery(this).attr('action') + '?s=' + encodeURIComponent(s) + '&action=search_ajax'
            jQuery.ajax({
                url: url,
                type: 'get',
                dataType: 'html',
                beforeSend: function () {
    
                    submit.attr('disabled', true).fadeTo('slow', 0.5);
                    document.body.style.cursor = 'wait';
                    var load = '<div id="content" role="main"><h1 class="page-title">Searching...</h1></div>';
                    jQuery('#container').empty().html(load);
                },
                success: function (data) {
                    submit.attr('disabled', false).fadeTo('slow', 1);
                    document.body.style.cursor = 'auto';
                    jQuery('#container').empty().html(data);
                    jQuery('#ajaxback').click(function () {
                        jQuery('#container').empty().html(contentCache);
                    });
    
                }
    
            });
            return false;
        });
        var timer, currentKey;
        jQuery('#s').keyup(function () {
            clearTimeout(timer);
            timer = setTimeout(function () {
                var sInput = jQuery('#s');
                var s = sInput.val();
                if (s.length == 0) {
                    if (searched) {
                        jQuery('#container').empty().html(contentCache);
                        sInput.focus();
                        //jQuery('#search-form span.processing').remove();
                        searched = false;
                    }
                    currentKey = s;
                } else {
                    if (s != currentKey) {
                        if (!searched) {
                            contentCache = jQuery('#container')[0].innerHTML;
                            searched = true;
                        }
                        currentKey = s;
                        if (s != ' ') {
                            jQuery('#searchform').submit();
                        }
                    }
                }
            }, 800);
        });
    
    });
    Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.

    My Latest Theme
  • borked
    Totally Borked
    • Feb 2005
    • 6284

    #2
    huh? Why do you want to clear the form? Why not have it clear the next time the cursor is clicked on the text field? That way the user still sees their search term in the text form?

    Anyway, a simple form reset should suffice no?

    onsubmit="this.submit(); this.reset();"

    For coding work - hit me up on andy // borkedcoder // com
    (consider figuring out the email as test #1)



    All models are wrong, but some are useful. George E.P. Box. p202

    Comment

    Working...