Ik probeer voor het eerst om object georienteerd te javascripten, en de DOM aan te passen ipv document.write's te gebruiken. Wat ik wil is het volgende:
Ik heb een pagina met daarop een textarea met als class "txtEditor". Een javascriptje scant de pagina en filtert alle textarea's met die class eruit. Vervolgens moet deze worden omgetoverd in een texteditor. In HTML moet het van dit
naar ongeveer dit:
Met andere woorden: ik wil een toolbar boven de textarea zetten en de naam van de textarea veranderen ivm css classes. Ik heb nu (onder andere) de volgende code:
Als ik de DOM bekijk, krijg ik wel de DIV en de UL te zien, maar de textarea is nergens te vinden. Wat doe ik verkeerd?
Ik heb een pagina met daarop een textarea met als class "txtEditor". Een javascriptje scant de pagina en filtert alle textarea's met die class eruit. Vervolgens moet deze worden omgetoverd in een texteditor. In HTML moet het van dit
HTML:
1
| <textarea name="bericht" class="textEditor">Testcontent</textarea> |
naar ongeveer dit:
HTML:
1
2
3
4
5
6
7
| <div id="bericht-txtContainer" class="bericht-txtContainer"> <ul> <li><a href="#" class="txtButtonBold">Bold</a></li> <li><a href="#" class="txtButtonLink">Hyperlink</a></li> </ul> <textarea name="bericht-txtTextarea" class="txtEditor">Testcontent</textarea> </div> |
Met andere woorden: ik wil een toolbar boven de textarea zetten en de naam van de textarea veranderen ivm css classes. Ik heb nu (onder andere) de volgende code:
JavaScript:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
| var txtToolbarItems = ['bold', 'link'] window.onload = init; function init() { var theTextareas = document.getElementsByTagName("textarea"); for (var i = 0; i < theTextareas.length; i++) { var theTextarea = theTextareas[i]; if (theTextarea.className.classExists("textEditor")) { if (theTextarea.id == "") { theTextarea.id = theTextarea.name; } new textEditor(theTextarea.id); } } } function textEditor(textareaID) { this.textarea = document.getElementById(textareaID); this.container = document.createElement("div"); this.container.id = this.textarea.id + "-txtContainer"; this.container.className = "-txtContainer"; this.toolbar = new txtToolbar(this); this.textarea.id += "-txtTextarea"; this.textarea.name += "-txtTextarea"; this.container.appendChild(this.toolbar.theList); this.container.style.visibility = "hidden"; this.textarea.parentNode.replaceChild(this.container, this.textarea); } function txtToolbar(theEditor) { this.txtEditorObject = theEditor; this.theList = document.createElement("ul"); this.theList.id = this.txtEditorObject.textarea.id + "txtToolbar"; this.theList.className = "txtToolbar"; this.theList.txtToolbarObject = this; for (var i = 0; i < txtToolbarItems.length; i++) { switch (txtToolbarItems[i]) { case "bold": this.addButton(this.theList.id + "ButtonBold", "txtButtonBold", "Bold", "bold"); break; case "link": this.addButton(this.theList.id + "ButtonLink", "txtButtonLink", "Hyperlink", "link"); break; } } } txtToolbar.prototype.addButton = function(theID, theClass, theLabel, theAction) { var menuItem = document.createElement("li"); var theLink = document.createElement("a"); var theText = document.createTextNode(theLabel); menuItem.id = theID; menuItem.className = "txtEditButton"; theLink.href = "#"; theLink.title = theLabel; theLink.className = theClass; theLink.action = theAction; theLink.appendChild(theText); menuItem.appendChild(theLink); this.theList.appendChild(menuItem); } |
Als ik de DOM bekijk, krijg ik wel de DIV en de UL te zien, maar de textarea is nergens te vinden. Wat doe ik verkeerd?
"Real software engineers work from 9 to 5, because that is the way the job is described in the formal spec. Working late would feel like using an undocumented external procedure."