using placeholders where value is empty

Gurpreet Singh Randhawa
Gurpreet Singh Randhawa used Ask the Experts™
on
I am using the form where in a situation i have to use place holder or title to insert the value in the database, i know we can use

$form.serialize which picks all the values of the form, but in my case value field is always going to be empty and i want to pick placeholders value and title value if placeholder is not defined just like serialization,

is there custom build jquery code or something already build in, how can i can use it just like $(form).serializePlaceholder or $(form).serializetITLE
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
There is not built in function but you could do something like this

Why don't you default the value of the control to the placeholder value if the value is empty when you render the form.

For inputs you could do something like this before you do the serialize
<script>
$(function() {
  $('form').submit(function() {
    // GET AN ARRAY OF ALL INPUTS THAT ARE EMPTY
    var emptyInputs = $('input', $(this)).filter(function() { return this.value ==""; });

    // ITERATE THROUGH THE ARRAY AND SET THE value TO THE 
    // placeholder ATTRIBUTE
    emptyInputs.each(function() {
      this.value = $(this).attr('placeholder');
    });  

    // NOW SERIALIZE
    data = $(this).serialize();
    return false;
  });
});
</script>

Open in new window

Working sample here
i will check it

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial