Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep placeholder visible when user is typing first substring of the placeholder text

Usually when we use input placeholder, the placeholder text will disappear as soon as the user type something.

enter image description here

I'm thinking to give user an random example on input so user can mimic using placeholder. The problem is that, when user type something, the example will disappear. Is there any way to keep the placeholder visible when user type the beginning part of the placeholder text?

Here's an example

enter image description here

The placeholder text is 'Lorem ipsum dolor sit amet'. When user types 'Lorem ipsum', I assume that user will try to type the example so the text will still be visible. But when user types something else like 'Lorem dolor', I assume user will try to type something different from the example. But if it turns out the user pressed wrong button, the placeholder text will be visible again after user pressed 'backspace' until the input text becomes the placeholder part again (e.g. user delete the 'dolor' text and the input text is back to 'Lorem').

Actually currently I use autocomplete dropdown as alternative, but I feel curious whether this can be done using Javascript (preferably jQuery).

UPDATE

This is as far as I can get. I'm thinking of cloning an element with same style as the text input.

Something like this

var textinput = $('#textinput');
var textplaceholder = $('<span>');
var placeholdertext = textinput.attr('placeholder');
textinput.attr('placeholder', '');

textplaceholder.insertAfter(textinput);
textplaceholder.html(placeholdertext);
textplaceholder.copyCSS(textinput);

textinput.keyup(function() {
    var current = $(this).val();
    if(placeholdertext.substr(0, current.length) == current){
        textplaceholder.html(placeholdertext);
    } else {
        textplaceholder.html(current);
    }
});

Here's the fiddle http://jsfiddle.net/petrabarus/FDS88/

The problem is how to make the element appear right behind the input text, make the text looks align in every browser, and mimic the mouse interaction (i.e. the border glow when the text is on focus, etc)?

like image 247
Petra Barus Avatar asked Apr 29 '14 23:04

Petra Barus


People also ask

How do you make a placeholder stay in HTML?

you can use <input title="//placeholder text" .../> that will remain there. or just show the input title <title for../> programaticaly when user starts typing.

How do you move placeholder to top on focus and while typing?

You can use the pointer-events property and the :focus selector to work your way around it with just CSS.

Can we use placeholder in textarea?

The placeholder attribute specifies a short hint that describes the expected value of a text area. The short hint is displayed in the text area before the user enters a value.


1 Answers

There are many ways to achieve that effect. This is one way of doing it:

<div class="wrapper">
    <span class="textbox" contentEditable="true">Lorem i</span><span class="gray"></span>
</div>
<!-- and some CSS magic to make it look legit -->
var text = "Lorem ipsum";

$(".wrapper > .textbox").on("input", function(){
    var ipt = $(this).text().replace(/\u00A0/g, " ");
    //freakin NO-BREAK SPACE needs extra care
    if(text.indexOf(ipt) == 0){
        $(".gray").text(text.substr(ipt.length, text.length));
    }else{
        $(".gray").text("");
    }
}).trigger("input");

http://jsfiddle.net/DerekL/7sD2r/

About the NO-BREAK SPACE, see here.

like image 144
Derek 朕會功夫 Avatar answered Sep 30 '22 08:09

Derek 朕會功夫