View Single Post
Old 03-04-2011, 09:00 AM  
fris
Too lazy to set a custom title
 
fris's Avatar
 
Industry Role:
Join Date: Aug 2002
Posts: 55,372
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.


WP Stuff
fris is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote