Smilie toevoegen in form

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Roy marijnissen

roy marijnissen

16/10/2012 15:22:28
Quote Anchor link
Ik heb onderstaande code waarmee je door een druk op de knop bold/italic/url tags kunnen toevoegen aan de textarea. Dit werkt allemaal prima. Maar nu wil ik een paar smilies op de zelfde manier toevoegen. Alleen krijg ik het niet werkend. Ik weet dat ik iets moet toevoegen aan de javascript code, ik weet alleen niet wat. Zou iemand me hiermee kunnen helpen ?

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
74
75
76
77
78
79
80
81
82
83
84
85
86
<SCRIPT language=Javascript>
function bbcode_ins(fieldId, tag)
{
field=document.getElementById(fieldId);
if(tag=='b' || tag=='i' || tag=='u' || tag == 'php' || tag == 'code')
{
if (document.selection)
{
field.focus();
sel = document.selection.createRange();
sel.text = '[' + tag + '][/' + tag+']';
}
//MOZILLA/NETSCAPE/SAFARI support
else if (field.selectionStart || field.selectionStart == 0)
{
var startPos = field.selectionStart;
var endPos = field.selectionEnd;
field.focus();
field.value = field.value.substring(0, startPos)
+ '[' + tag + '][/' + tag+']'
+ field.value.substring(endPos, field.value.length);
}
}
else if(tag == 'img')
{
var path = prompt('Enter image path', 'http://');
if(!path)
{
return;
}
if (document.selection)
{
field.focus();
sel = document.selection.createRange();
sel.text = '[' + tag + ']' + path + '[/' + tag+']';
}
//MOZILLA/NETSCAPE/SAFARI support
else if (field.selectionStart || field.selectionStart == 0)
{
var startPos = field.selectionStart;
var endPos = field.selectionEnd;
field.focus();
field.value = field.value.substring(0, startPos)
+ '[' + tag + ']' + path + '[/' + tag+']'
+ field.value.substring(endPos, field.value.length);
}
}
else if(tag == 'url')
{
var url = prompt('Enter link URL', 'http://');
var linkText = prompt('Enter link text', '');
if(!url || !linkText)
{
return;
}
if (document.selection)
{
field.focus();
sel = document.selection.createRange();
sel.text = '[' + tag + '='+url+']' + linkText + '[/' + tag+']';
}
//MOZILLA/NETSCAPE/SAFARI support
else if (field.selectionStart || field.selectionStart == 0)
{
var startPos = field.selectionStart;
var endPos = field.selectionEnd;
field.focus();
field.value = field.value.substring(0, startPos)
+ '[' + tag + '='+url+']' + linkText + '[/' + tag+']'
+ field.value.substring(endPos, field.value.length);
}
}
}
</script>
<form>
<input type="button" onclick="bbcode_ins('comment', 'b')" value="B" style="width:15px;font-weight:bold;" />
<input type="button" onclick="bbcode_ins('comment', 'u')" value="_" style="width:15px;" />
<input type="button" onclick="bbcode_ins('comment', 'i')" value="I" style="width:15px;font-style:italic;" />
<input type="button" onclick="bbcode_ins('comment', 'img')" value="img" style="width:25px;" />
<input type="button" onclick="bbcode_ins('comment', 'url')" value="url" style="width:25px;" />
<input type="button" onclick="bbcode_ins('comment', 'php')" value="php" style="width:25px;" />
<input type="button" onclick="bbcode_ins('comment', 'code')" value="code" style="width:30px;" />
<input type="button" onclick="bbcode_ins('comment', ':)')" value=":)" style="width:30px;" />

<textarea name='comment' cols='45' rows='10' id="comment"></textarea>
</form>
 
PHP hulp

PHP hulp

21/09/2024 05:21:54
 
- Ariën  -
Beheerder

- Ariën -

16/10/2012 15:34:58
Quote Anchor link
Welke browser? Al in de debug/ontwikkelaars console gekeken?
 
Roy marijnissen

roy marijnissen

16/10/2012 15:40:35
Quote Anchor link
Firefox. Maar ik weet wel waar het fout gaat, alleen niet hoe ik dit kan oplossen. Onderstaand stukje van de code zorgt ervoor dat als je b.v. de 'b' button klikt hij <b></b> toevoegd aan het form. Dit moet ik dus ook doen voor de smilies. Alleen heb ik geen idee hoe ik het script moet aanpassen zodat dit werkt.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
field=document.getElementById(fieldId);
if(tag=='b' || tag=='i' || tag=='u' || tag == 'php' || tag == 'code')
{
if (document.selection)
{
field.focus();
sel = document.selection.createRange();
sel.text = '[' + tag + '][/' + tag+']';
}
 
Kris Peeters

Kris Peeters

17/10/2012 11:04:53
Quote Anchor link
Bij BBcode heb je een selectie. Voor een smiley heb je enkel de cursor positie nodig. Dus de smiley is een stuk eenvoudiger.
Maak er een aparte functie van (anders dekt de naam van de functie de lading niet meer).
Zoiets (ik heb zo veel mogelijk de code hergebruikt)
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<script>
function insert_smiley(fieldId, tag)
{
  var field=document.getElementById(fieldId);
  if (document.selection)
  {
    field.focus();
    sel = document.selection.createRange();
    sel.text = tag ;
  }
  //MOZILLA/NETSCAPE/SAFARI support
  else if (field.selectionStart || field.selectionStart == 0)
  {
    var startPos = field.selectionStart;
    var endPos = field.selectionEnd;
    field.focus();
    field.value = field.value.substring(0, startPos)
    + tag
    + field.value.substring(endPos, field.value.length);
  }

}
</script>
<input type="button" onclick="insert_smiley('comment', ':)')" value=":)" style="width:30px;" />
<textarea name='comment' cols='45' rows='10' id="comment"></textarea>
Gewijzigd op 17/10/2012 11:06:41 door Kris Peeters
 
Roy marijnissen

roy marijnissen

17/10/2012 11:37:28
Quote Anchor link
Werkt perfect, bedankt !!
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.