jeudi 28 avril 2016

Adding bullet points to multiple textareas with same Javascript

I found the below code online to add bullet points to a textarea, and it works quite well for a single textarea.

Script

var CRLF = 10;
var BULLET = String.fromCharCode(45);

function Init() {
    if (txt.addEventListener) txt.addEventListener("input", OnInput, false);
}

function OnInput(event) {
    char = event.target.value.substr(-1).charCodeAt(0);
    nowLen = txt.value.length;

    if (nowLen > prevLen.value) {
        if (char == CRLF) txt.value = txt.value + BULLET + " ";
        if (nowLen == 1) txt.value = BULLET + " " + txt.value;
    }
    prevLen.value = nowLen;
}

HTML

<body onload="Init ();">
    <h4>Automatic bullets in a text box</h4>
    <textarea id="txt" oninput="OnInput(this, 'prevLen');" rows="15" cols="40"></textarea>
    <input type="hidden" id="prevLen" value="0"/>
</body>

However, I can't figure out how to create a similar function such that I can use it on multiple textareas.

I would like something where I can pass through the id of the hidden input, so I can specify that way which input to add the bullet points to, but can't get a working solution.

Suggestions/solutions welcome.




Aucun commentaire:

Enregistrer un commentaire