user3274745
user3274745

Reputation: 361

How to use empty placeholder text

Well the question is pretty clear. How could i use text from a placeholder e.g from the below text. I've been searching a lot for this and haven't found a solution. Your help would be appreciated.

<input type="text" placeholder="YYYY, MM - 1, DD" name="director_date"
 value="<?php print get_option('director_date');
 ?>" />

Upvotes: 1

Views: 943

Answers (4)

smistry
smistry

Reputation: 1126

You could do something like this

$('form').submit(function(){
    var input = $(this).find('input[name=box]');
    if(input.val() == ""){
       input.val(input.attr('placeholder'));       
    };
});

DEMO FIDDLE (ignore the alert and return false): http://jsfiddle.net/TwY3E/

Upvotes: 1

lafor
lafor

Reputation: 12776

Use your "placeholder" as the input's default value, then throw in some javascript to clear the value on focus and restore it on blur if empty.

<input type="text" onfocus="if(this.value=='placeholder text') this.value='';" onblur="if(this.value=='') this.value='placeholder text';" value="placeholder text">

JSFiddle demo

Upvotes: 1

Travis Hegner
Travis Hegner

Reputation: 2495

For a non javascript solution, you'll have to test for an empty field on form submittal.

<?php
if(empty($_REQUEST['director_date']))
{
    $dir_date = "YYYY, MM - 1, DD"; //Set desired date here.
} else {
    $dir_date = $_REQUEST['director_date'];
}

Upvotes: 2

potashin
potashin

Reputation: 44581

My first thought was to use javascript. However, you should use ajax or something to send it to the server.

function getMe(){
  var input = document.getElementsByTagName('input');
  var final = [];
  for(var i = 0; i<input.length;i++){
  if(input[i] == ''){
       final[input[i].name] = input[i].placeholder;
    } else {
       final[input[i].name] = input[i].value;
    }
  }
}

Upvotes: 1

Related Questions