#{b...">

CodeMirror markText not working

I am using CodeMirror like this to show some XML response to the user.

HTML code

<body> <textarea id="cm" >#{bean.xmlResponse}</textarea> </body> 

Js code

 window.onload = function () { var editor = CodeMirror.fromTextArea(document.getElementById('cm'), { mode: "xml", theme: "default" }); editor.getDoc().markText({line:5,ch:2},{line:5,ch:9},"color : red"); }; 

Now, when I try to highlight a specific line using markText, which does not work. Of course, the "xml" mode works, but line 5 is not highlighted in red.

I really appreciate your help. It has been 3 days trying to do this. Thanks.

+5
source share
1 answer

You need to specify the options parameter as a map, not a line: {css: "color : red"}

See the documentation for more details: https://codemirror.net/doc/manual.html#markText

Here is a snippet based on your example that shows that it works when you describe (you can ignore unnecessary CSS / JS settings and the xml example that should have run the snippet):

 var editor = CodeMirror.fromTextArea(document.getElementById('cm'), { mode: "xml", theme: "default" }); editor.getDoc().markText({ line: 5, ch: 10 }, { line: 5, ch: 39 }, { css: "color : red" }); 
 @import "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.12.0/codemirror.css" 
 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.12.0/codemirror.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.12.0/mode/xml/xml.js"></script> <textarea id="cm"> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </textarea> 
+6
source

Source: https://habr.com/ru/post/1245198/