Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8951677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:43:10+00:00 2026-06-15T13:43:10+00:00

I want my TextArea to be able to support multiple CodeMirror modes. For now

  • 0

I want my TextArea to be able to support multiple CodeMirror modes. For now I want it to support json and xml. Is this possible?
Also, is it possible to automatically detect whether the user placed json or xml in the area?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:43:11+00:00Added an answer on June 15, 2026 at 1:43 pm

    CodeMirror actually has an example very close to what you are looking for here.

    Here is a more specific example that does what you want.

    1. Create a CodeMirror instance.
    2. When the content changes we determine if we should switch modes.

    The logic I put in for determining what mode you are in is very simplistic and can be refactored to support as robust a check as you deem appropriate for either mode. (Regex is good for complex checking if you want to get fancy…that is the only reason I used it even in my simple example) Currently, my example code just checks for any content where the first non-space character is “<” thus indicating xml mode. When deciding to switch back it just checks that the first non-space character is not “<” and that it is not blank (in case the user just deleted everything to start over with more xml).

    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Code Mirror Example</title>
    <script src="lib/codemirror.js"></script>
    <link rel="stylesheet" href="lib/codemirror.css">
    <script src="mode/javascript/javascript.js"></script>
    <script src="mode/xml/xml.js"></script>
    <style type="text/css">.CodeMirror{border:1px solid black;}</style>
    </head>
    <body>
        <div><span>Mode: </span><span id="modeType">JSON</span></div>
        <textarea class='codeEditor'></textarea>
    
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        function determineCodeMirrorType(cm)
        {
            if (cm.getMode().name == 'javascript')
            {
                checkAndSwitchToXML(cm, cm.getValue());
            }
            else if (cm.getMode().name == 'xml')
            {
                checkAndSwitchToJSON(cm, cm.getValue());
            }
        }
    
        function checkAndSwitchToXML(cm, val)
        {
            if (/^\s*</.test(val))
            {
                cm.setOption("mode", "xml");
                $('#modeType').html("XML");
            }
        }
        function checkAndSwitchToJSON(cm, val)
        {
            if (!/^\s*</.test(val) && val.match(/\S/))
            {
                cm.setOption("mode", "javascript");
                $('#modeType').html("JSON");
            }
        }
    
        function buildCMInstance(mode, value)
        {
            var cm = CodeMirror.fromTextArea($('.codeEditor')[0], {
                mode:mode,
                value:value,
                lineNumbers:true,
                onChange:function(cmInstance){
                    determineCodeMirrorType(cmInstance);
                }
            });
            return cm;
        }
        $(document).ready(function(){
            //mode changing demo:  "http://codemirror.net/demo/changemode.html";
            var cm = buildCMInstance("javascript");
        });
        </script>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want a textarea with no scrollbars. This is done by setting overflow to
So I have a textarea containing text. I want to be able to display
I want to insert TAB characters inside a TEXTAREA, like this: <textarea>{KEYPRESS-INSERTS-TAB-HERE}Hello World</textarea> I
I want to be able to count the number of characters in a textarea,
I have a textarea field and I want the user to be not able
I have a HTML <textarea> that I want to be able to make when
I have a textarea within a div. I want to be able to click
I want to be able to show additional text in a textarea, like so
i want update textarea content onclock on a input button for example: <input type=button
I want to send a textarea's data when user presses shift + enter ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.