-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathexample.js
48 lines (41 loc) · 1.07 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
$(function () {
"use strict";
$(".example").each(function (idx, el) {
var textarea = $("textarea", el)[0];
var htmlElement = $($(".example-html", el).get(0));
var runButton = $($("button.run", el).get(-1));
var htmlContent = htmlElement.html();
if (!textarea || !htmlElement) {
return;
}
var cm = CodeMirror.fromTextArea(textarea, {
mode: "javascript",
styleSelectedText: true,
// lineNumbers: true,
});
function run() {
var content = cm.getValue();
try {
// reload content of html-element
htmlElement.html(htmlContent);
// eval script
eval(content);
runButton.removeClass("error");
} catch (e) {
console.error(e);
runButton.addClass("error");
}
}
run();
runButton.click(run);
});
$("textarea.code").each(function (idx, el) {
var $el = $(el);
$el.html($el.html().trim());
var cm = CodeMirror.fromTextArea(el, {
mode: "javascript",
styleSelectedText: true,
// lineNumbers: true,
});
});
});