tags:

views:

223

answers:

4

In my site, I would like to implement a textbox where people can input a set of strings separated by a separator character.
For example the tags textbox at the bottom of this page: tags(strings) delimited by space(separator).
To make it more clear to the user, it would make a lot of sence to give each string a different background color or other visual hint.
I don't think this is possible with a regular input[text] control.

Do you deem it possible to create something like that with javascript? Has somebody done this before me already? Do you have any other suggestions?

A: 

You can look how they do that at scripts like TinyMCE, which add such features to textareas. In textareas you can use HTML to colorize text.

Ikke
I don't think a full blown editor is what I'm looking for, but indeed, I could probably check out how they did it.
borisCallens
Should have been more clear. Thats what I meant.
Ikke
Yes, I deduced that :) It is indeed a good idea to have a peek.
borisCallens
A: 

You can use multiple textboxes

textbox1 <space> textbox2 <space> textbox3 ....

and so on... You can then apply the background-color style to each textbox.

Kirtan
unless I can make them look like they are only one textbox, I think it will make the interface more confusing rather then the other way round. But I have been thinking along that road too.
borisCallens
+1  A: 

This is quite interesting. The short answer to your question is no. Not with the basic input element.

The real answer is: Maybe with some trickery with javascript.

Apparently Facebook does something close to this. When you write a new message to multiple persons in Facebook, you can type their names this sort of way. Each recognized new name is added a bit like an tag here and has an small cross next to it for removing it.

What they seem to do, is fake the input area size by drawing an input-looking box and removing all styling from the actual input with css. Then they have plenty of logic done with javascript so that if you have added an friend as a tag and start backspacing, it will remove the whole friends name at once. etc.

So, yes, it's doable, but takes plenty of effort and adds accessibility problems.

Mikko Tapionlinna
Yes, I guessed it would be a lot of work so I secretly hoped that something like this was already jqueried somewhere ;) But I think it is worth the effort for it would add a lot of value to quite a lot of sites actually.
borisCallens
+4  A: 

Basic Steps

  1. Put a textbox in a div and style it too hide it.
  2. Make the div look like a text box.
  3. In the onClick handler of the div, set the input focus to the hidden text box.
  4. Handle the onKeyUp event of the hidden text box to capture text, format as necessary and alter the innerHtml of the div.

Tis quite straightforward. I'll leave you to write your formatter but basically you'd just splitString on separator as per the Semi-Working-Example.

Simple Outline

<html>
    <head>
    <script language="javascript" type="text/javascript">
    function focusHiddenInput()
    {
        var txt = document.getElementById("txtHidden");
        txt.focus();
    }

    function formatInputAndDumpToDiv()
    {
        alert('Up to you how to format');
    }
    </script>
    </head>

    <body>
    <div onclick="focusHiddenInput();">
    Some label here followed by a divved textbox:
    <input id="txtHidden" style="width:0px;" onKeyPress="formatInputAndDumpToDiv()" type="text">
    </div>
    </body>
    </html>

Semi-Working Example

You still need to extend the click handlers to account for tag deletion/editing/backspacing/etc via keyboard.... or you could just use a click event to pop up another context menu div. But with tags and spacer ids identified in the code below that should be pretty easy:

<html>
    <head>
    <script language="javascript" type="text/javascript">

    var myTags=null;

    function init()
    {
        document.getElementById("txtHidden").onkeyup= runFormatter;
    }

    function focusHiddenInput()
    {
        document.getElementById("txtHidden").focus();
    }

    function runFormatter()
    {
        var txt = document.getElementById("txtHidden");
        var txtdiv = document.getElementById("txtBoxDiv");
        txtdiv.innerHTML = "";
        formatText(txt.value, txtdiv);
    }

    function formatText(tagText, divTextBox)
    {
        var tagString="";
        var newTag;
        var newSpace;
        myTags = tagText.split(' ');
        for(i=0;i<myTags.length;i++) {
            newTag = document.createElement("span");
            newTag.setAttribute("id", "tagId_" + i);
            newTag.setAttribute("title", myTags[i]);
            newTag.setAttribute("innerText", myTags[i]);

            if ((i % 2)==0) {
        newTag.style.backgroundColor='#eee999';  
     }
     else
     {
        newTag.style.backgroundColor='#ccceee'; 
     }
            divTextBox.appendChild(newTag);
            newTag.onclick = function(){tagClickedHandler(this);}

            newSpace = document.createElement("span");
            newSpace.setAttribute("id", "spId_" + i);
            newSpace.setAttribute("innerText", " ");
            divTextBox.appendChild(newSpace);

            newSpace.onclick = function(){spaceClickedHandler(this);}
       }
    }

    function tagClickedHandler(tag)
    {
      alert('You clicked a tag:' + tag.title); 
    } 

    function spaceClickedHandler(spacer)
    {
      alert('You clicked a spacer'); 
    } 

    window.onload=init;
    </script>
    </head>

    <body>
    <div id="txtBoxDivContainer">
    Enter tags below (Click and Type):<div id="txtBoxDiv" style="border: solid 1px #cccccc; height:20px;width:400px;" onclick="focusHiddenInput();"></div>
    <input id="txtHidden" style="width:0px;" type="text">
    </div>
    </body>
    </html>

Cursor

You could CSS the cursor using blink (check support) or otherwise just advance and hide as necessary an animated gif.

rism
Yes, that could work :)
borisCallens