Search code examples
javascriptjquerycodemirror

CodeMirror document.getElementById on all id's with x before hyphen


I have a few textareas on the page where i am running CodeMirror to do some syntax highlighting and enable editing of code.

Currently i select one textarea via: CodeMirror.fromTextArea(document.getElementById("code") but my text areas are numerical and start from 1 so my id's are as follows:

code-1
code-2

How would i use something like div[id|='code'] which i believe will look at everything before the hyphen? I assume i'd need something different from getElementById for this?


Solution

  • You can use jQuery's 'attribute begins with' selector for this:

    $('textarea[id^=code-]').each(function() {
        CodeMirror.fromTextArea(this);
    });