Ik wil, met behulp van een knop, tekst invoeren op de plek waar de cursor het laatst heeft gestaan in een textarea. Ik heb het al bijna werkend, maar zoals ik het nu heb, dan komt de tekst helemaal op het eind van de textarea. Ik wil dus dat de tekst komt te staan op de plek waar ik het laatst in de textarea geklikt heb. Is dit mogelijk? Eerlijk gezegd betwijfel ik het, omdat de textarea niet meer geselecteerd is op het moment dat ik ergens anders op het scherm klik.
Mja opzich niet zo moeilijk. Kijk in de source van de post reply in dit topic. Als je een smiley invoegt staat het daar.
Yep, de functie putStr:
JavaScript:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| function putStr( text ) { var target = document.getElementById( "messageBox" ); if ( target ) { if ( document.all && target.cursorPos) { var cursorPos = target.cursorPos; cursorPos.text = cursorPos.text.charAt(cursorPos.text.length - 1) == ' ' ? text + ' ' : text; target.focus(); } else { target.value += text; target.focus(); } } } |
[ Voor 6% gewijzigd door André op 24-08-2003 20:25 ]
De nieuwe functie putStr:
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
| function putStr( text ) { var target = document.getElementById( "messageBox" ); if ( target ) { if ( document.all && target.cursorPos ) { var cursorPos = target.cursorPos; cursorPos.text = cursorPos.text.charAt(cursorPos.text.length - 1) == ' ' ? text + ' ' : text; } else if ( typeof target.selectionStart != 'undefined' ) { var sStart = target.selectionStart; var sEnd = target.selectionEnd; target.value = target.value.substr(0, sStart) + text + target.value.substr(sEnd, target.value.length); target.selectionStart = sStart == sEnd ? sStart + text.length : sStart; target.selectionEnd = sStart + text.length; } else { target.value += text; } target.focus(); } } |
Intentionally left blank
Hm dit werkt niet bij mij. Die target.cursorPos geeft steeds niks terug. Ik kom maw steeds terecht in de laatste else. Het zal waarschijnlijk schelen dat ik eerst nog een ander input-veld invul met de tekst die ik met de knop wil toevoegen. Voor de duidelijkheid kun je het hier bekijken: http://131.155.240.208/berson/admin/toevoegen.html
je moet je cursorpositie ook bijhouden:
en je textarea:
volledig voorbeeld
JavaScript:
1
2
3
| function storeCursor(el) { if (document.all && el.createTextRange) el.cursorPos = document.selection.createRange().duplicate(); } |
en je textarea:
HTML:
1
2
3
4
| <textarea id="messageBox" onselect="storeCursor(this)" onkeyup="storeCursor(this)" onclick="storeCursor(this)"></textarea> |
volledig voorbeeld
[ Voor 5% gewijzigd door crisp op 24-08-2003 21:09 ]
Intentionally left blank
Hee thx het werkt nu. Vaag maar ik kan de source van GoT niet bekijken, krijg dan een lege textfile. Is dat een beveiliging??
dan zit je cache te vol (IE bugje); cache leeggooien zou moeten helpenDr_Frickin_Evil schreef op 24 August 2003 @ 21:42:
Hee thx het werkt nu. Vaag maar ik kan de source van GoT niet bekijken, krijg dan een lege textfile. Is dat een beveiliging??
Intentionally left blank
Pagina: 1