views:

62

answers:

3

Hi, i have a little problem with Codemirror (http://codemirror.net/manual.html)

My code

$(document).ready(function ust()
    {    
        pa_textareas();

    });


    function pa_textareas()
    {
            var textarea = document.getElementById('ta_1');
          var editor_1 = new MirrorFrame(CodeMirror.replace(textarea), {
            height: "100%",
            width: "100%",                
            parserfile: ["tokenizejavascript.js", "parsejavascript.js"],
            stylesheet: "js/js_parser/jscolors.css",
            path: "js/js_parser/",
            autoMatchParens: false,
            content: 'test kjskljsklj skjs lkj slkj sl',
            initCallback: function(getContents) { e1_ct  = ''+editor_1.mirror.getCode()+''; },
            onChange: function (n) { e1_ct = ''+editor_1.mirror.getCode()+''; }
          });

          var textarea = document.getElementById('ta_2');
          var editor_2 = new MirrorFrame(CodeMirror.replace(textarea), {
            height: "100%",
            width: "100%",                
            parserfile: ["tokenizejavascript.js", "parsecss.js"],
            stylesheet: "js/js_parser/csscolors.css",
            path: "js/js_parser/",
            autoMatchParens: false,
            content: 'blub kasjdkljas dkjas lkdj alskj dlk',
            initCallback: function(getContents) { e2_ct  = ''+editor_2.mirror.getCode()+''; },
            onChange: function (n) { e2_ct = ''+editor_2.mirror.getCode()+''; }
          });

    }

I want to insert a button wich format the code but i get always an error.

Button: onclick="editor_2.mirror.reindent();"

Result: -> "editor_2 is not defined"

Thx Peter :)

edit: code fixed

+2  A: 

Because there is no point in your code where you assign a value to editor_code_2, but you try to use it twice.

David Dorward
Not only that. But he also needs to be sure where ever he defines the illusive "editor_code_2", is outside of his DOM ready closure.
Chase
sorry, that was a mistake in the example code. It is editor_2
Peter
A: 

I cannot find anywhere in your code where you create a variable called editor_code_2. This is why you are getting the error. It seems you have mispelled your editor_2 variable.

Darko Z
sorry, that was a mistake in the example code. It is editor_2.
Peter
+2  A: 

The scope of editor_2 is the function pa_textareas. Since you are calling it using an intrinsic event handler attribute, you can't reach the variable.

You should probably be assigning the event handler with JavaScript inside the pa_textareas function.

Since you are using jQuery (At least I assume you are, there are other libraries with the stupid $ variable):

jQuery('some selector').click(function () { editor_2.mirror.reindent(); });

Since the anonymous function is declared inside pa_textareas it will have access to variables defined in that scope.

David Dorward
I understand, logical. Thx for the fast help. Btw: Why is the $ stupid?
Peter
Because it is (a) Meaningless, (b) Used by different common libraries to mean different things, and (c) reserved (in the ECMAScript standard) for machine generated code.
David Dorward