Each time I enter a character in the input field, I parse the expression so as to get:
- Array of tokens i.e. tokenArray = [2, *, COS, (, 4,)]
- The corresponding array of token types, i.e. tokenType = [NUMBER, OPERATOR, FUNCTION, (, NUMBER,)]
I need to style each token (for example, assigning a different color to each token) based on its corresponding type of token.
I was able to easily create a dynamic copy of the input text in another <div> (and not in the input text that I ask for help) as follows:
JavaScript:
function processExpression() { var userInput = document.getElementById('inputText').value; var resultOutput = parse(userInput);
HTML:
<input type="text" id="inputText" value="" placeholder="Type expression" size="40" onKeyDown="processExpression();" onKeyUp="processExpression();"/> <div id="styledExpression" value=""></div>
How to style input text directly in the input field where I type? Any javascript solution?
UPDATE
Tim's answer to the question of replacing innerHTML in a contenteditable div gives some useful help.
How would you modify http://jsfiddle.net/2rTA5/2/ to decide when, each time you click on a key, one repeats the entire editable one? For example, imagine that you enter "= if (AND (3 = 2, A1: A4, OR (0,1)), 5,6)", and each time you press the key, the edited program code is overwritten (see), and i'm losing the cursor.
How can this solution ignore the type of token or symbol or node and just save and restore the absolute pointer (from the beginning) that was before the key was pressed?
Gian
source share