Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Span inside text input field?

Is it somehow possible to place a span as the value of a text input field?

I am making a mailing system for a website and want a nice looking receivers input field, where added receivers are contained and added to the value of input text field. At the moment i use a separate "adding field" while showing added receivers in a span-container. I want to merge these to fields together. Just like any input field in regular e-mail software.

Help would be most appreciated! Thanks in advance!

like image 286
Snorreri Avatar asked Jun 27 '11 21:06

Snorreri


2 Answers

Short answer: no, you cannot include a <span /> within an <input />.

You have a few options. You could use javascript to emulate behaviour like the email To: field. E.g. listen to key presses and handle actions like backspace after a ;.

Another option would be to make a list appear (css styled) like a textbox. Have the last <li /> contain a textbox with cleared styles. Every time the user adds a new email then insert a new <li /> before the textbox.

E.G.

html:

<ul class="email-textbox">
    <li>[email protected];</li>
    <li>[email protected];</li>
    <li><input type="text" /></li>
</ul>

css:

.email-textbox {
    background-color: #fff;
    border: 1px solid #ccc;
    padding: 2px 4px; 
}

.email-textbox li {
    display: inline-block;
    list-style: none;
    margin-left: 5px;   
}

.email-textbox input {
    background: none;
    border: none;
}

javascript (jQuery, can change to vanilla)

$(function () {
    $('.email-textbox').find('input').focus();
});

You will need to extend this javascript to include a keypress handler etc, but it gives the general idea.

Demo: http://jsfiddle.net/UeTDw/1/

Any option will require some javascript however.

If you can use jQuery, you could check out jQuery UI autocomplete

like image 199
jacob.toye Avatar answered Sep 26 '22 21:09

jacob.toye


One way to do it would be to layer a text input on top of a div that is styled to look like a text input.

<div id="fake-input">
    <span class="input-item">John Doe</span>
    <span class="input-item">Jane Deere</span>
    <input id="receiver-input" type="text" />
</div>

You can strip all styling off of receiver-input, and add borders, background colors, and such to fake-input so that it appears to be a text field. When a receiver is added, you can create a new input-item span and append it to the list.

like image 34
Jordan Avatar answered Sep 26 '22 21:09

Jordan