0

Basically I need to Set the input placeholder to last value typed into input after 1 second has passed. The reason is that if the user once again deletes everything from it, the placeholder will now show the last value typed into input after last autoupdate AJAX call AND NOT the placeholder value loaded during original page load.

WORKING CODE

  function autoSave(client_id, project_id, mainsheet_id, t_id, t_val) {

    if (t_val != '') {

      // Refresh variables just in case.
      client_id = $('#CLIENT_ID').val();
        project_id = $('#PROJECT_ID').val();
        mainsheet_id = $('#MAINSHEET_ID').val();

            // AJAX POST request to run MySQL UPDATE query on this database field ( WTRESRVD ).
            $.ajax({
                url: "processor.php",
                method: "GET",
                data: {
                    postCLIENT_ID: client_id,
                    postPROJECT_ID: project_id,
                    postMAINSHEET_ID: mainsheet_id,
                    postT_ID: t_id,
                    postT_VAL: t_val
                },
                dataType: "text",
                beforeSend: function() {
                    // setting a timeout
                    $('#status').text('Please wait...');

                },

                success: function(data) {
                    // If data return after a successful request isn't an empty string..
                  // ADD SERVER RESPONSE HANDING HERE.

                  /* Status Codes

                  return 0 = Nothing to Update
                  return 1 = Successful Update Query
                  return 2 = Database Connection refused
                  return 3 = MySQL Query Error OR Wrong URL Parameters */

                  // If data return 0 = Nothing to Update
                  if (data == '0') {

                  $('#status').text("Status: Nothing changed. Nothing saved.").show();
                  // ..fadeOut over 3 seconds. 
                  $('#status').fadeOut(5000);

                  }

                  // If data return 1 = Successful Update Query
                  if (data == '1') {

                  // Create variable time to reference later.
                  var time = showTime();
                  // Update div status with last saved time stamp then..
                  $('#status').text("Status: Saved @ " + time).show();
                  // ..fadeOut over 3 seconds. 
                  $('#status').fadeOut(5000);

                  }

                  // return 2 = Database Connection refused
                  if (data == '2') {

                  $('#status').text("Status: Database Connection refused.").show();
                  // ..fadeOut over 3 seconds. 
                  $('#status').fadeOut(5000);

                  }

                  // return 3 = MySQL Query Error OR Wrong URL Parameters
                  if (data == '3') {

                  $('#status').text("Status: MySQL Query Error OR Wrong URL Parameters.").show();
                  // ..fadeOut over 3 seconds. 
                  $('#status').fadeOut(5000);

                  }

                }

            });
        } else {

        // like this maybe???
        // not working...
        $(t_val).attr("placeholder", t_val);

        }

  }
suchislife
  • 4,251
  • 10
  • 47
  • 78
  • What is your value `t_val`? Does it correspond to the element, or to its value? If it's the element, it's likely that you're missing a hashtag in your jQuery selector `$(t_val).attr("placeholder", t_val);`. Also, you're probably looking for `t_val.val()` in the `attr()`. You're looking for something like `$(#field).attr("placeholder", "string");`. – Obsidian Age Aug 07 '17 at 04:29
  • So.. This looks promising. – suchislife Aug 07 '17 at 13:09

1 Answers1

0

You can use .queue() and .delay() to perform a task in N seconds

$("#selector").delay(1000, "placeholder").queue("placeholder", function() {
  $(this).prop("placeholder", "value" /* set placeholder value here */);
}).dequeue("placeholder");
guest271314
  • 1
  • 15
  • 104
  • 177
  • I don't really understand this code. Can you incorporate it into mine? – suchislife Aug 07 '17 at 04:52
  • @Vini Not sure what you mean. You are using the code at `else` statement, yes? – guest271314 Aug 07 '17 at 05:06
  • The code at else is just an approximation. Precisely what I am looking for as an example. – suchislife Aug 07 '17 at 05:10
  • `.queue()` queues a function to be called when `.dequeue()` is called, or when the function that is passed as parameter to `.queue()` `next` parameter which is a function is called. When the queue has a `queueName`, at Answer `"placeholder"`, it is possible to delay the call to `.dequeue()` by using the same `queueName` at second parameter to `.delay()` call, see https://stackoverflow.com/questions/29022701/change-easing-functions-on-animations-in-jquery-queue/ – guest271314 Aug 07 '17 at 06:02