domino
domino

Reputation: 7345

Jquery removing default text from textbox on submit

     $('input[class*="defaulttext"]').each(function () {

                    this.value = $(this).attr('title');
                    $(this).addClass('defaultclass');

                    $(this).focus(function () {
                        if (this.value == $(this).attr('title')) {
                            this.value = '';
                            $(this).removeClass('defaultclass');
                        }
                    });

                    $(this).blur(function () {
                    if (this.value == '') {
                        this.value = $(this).attr('title');
                        $(this).addClass('defaultclass');
                    }
                });


                });

This basically adds the background text effect for textboxes (uses the title). Once selected the text goes away.

Now, I have a problem with optional fields. If the user does not edit these fields the default background text is submitted with the form. So I need to reset the value of the unedited field once the form is submitted. Any ideas how to do this?

Maybe on form submit, check if the title = value? If yes then set to ''.

Upvotes: 2

Views: 1322

Answers (4)

naspinski
naspinski

Reputation: 34689

$("#yourForm").submit(function () {
    $('input.defaulttext').each(function() {
        if($(this).val() === $(this).attr('title')) { $(this).val(''); }
    });
};

Upvotes: 0

Mathew Thompson
Mathew Thompson

Reputation: 56429

Based on the code you have, you could do:

$("#yourForm").submit(function () {
     $('input[class*="defaulttext"]').val("");
};

Upvotes: 1

aldux
aldux

Reputation: 2804

You could do that, and check for values where title=value. Or alternatively, you could mark your edited fields using jQuery.data()

Upvotes: 0

Santosh Gokak
Santosh Gokak

Reputation: 3411

We used HTML5 Placeholder jQuery Plugin and it worked well in this case.

Upvotes: 1

Related Questions