CodeMirror find and replace - Javascript CodeMirror

Javascript examples for CodeMirror:Configuration

Description

CodeMirror find and replace

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.js"></script> 
      <style id="compiled-css" type="text/css">

.hl {/*  w w w.java 2s.co m*/
   background-color: #FFFF00;
}


      </style> 
      <script type="text/javascript">
    $(window).load(function(){
var editor = CodeMirror.fromTextArea(document.getElementById("myTextarea"), {
    mode: "text/html",
    lineNumbers: true
});
document.querySelector("#myButton").onclick = function(evt) {
    var markerClassName = 'hl';
    var value = $('#search').val();
    var cursor = editor.getSearchCursor(value);
    var first, from, to;
    while (cursor.findNext()) {
        from = cursor.from();
        to = cursor.to();
        editor.markText(from, to, {
            className: markerClassName
        });
        if (first === undefined) {
            first = from;
        }
    }
    if (first) {
        editor.scrollIntoView(first);
    }
    evt.preventDefault();
};
    });

      </script> 
   </head> 
   <body> 
      <link rel="stylesheet" href="https://codemirror.net/lib/codemirror.css"> 
      <link rel="stylesheet" href="https://codemirror.net/addon/dialog/dialog.css"> 
      <link rel="stylesheet" href="https://codemirror.net/addon/search/matchesonscrollbar.css"> 
      <script src="https://codemirror.net/lib/codemirror.js"></script> 
      <script src="https://codemirror.net/mode/xml/xml.js"></script> 
      <script src="https://codemirror.net/addon/dialog/dialog.js"></script> 
      <script src="https://codemirror.net/addon/search/searchcursor.js"></script> 
      <script src="https://codemirror.net/addon/search/search.js"></script> 
      <script src="https://codemirror.net/addon/scroll/annotatescrollbar.js"></script> 
      <script src="https://codemirror.net/addon/search/matchesonscrollbar.js"></script> 
      <input id="search" type="search"> 
      <button id="myButton">find</button> 
      <textarea id="myTextarea">hello world hello world</textarea>  
   </body>
</html>

Related Tutorials