Jump to content

Can anyone help me with my clickable smilie buttons


liam1412

Recommended Posts

Im trying to create the clickable smilie and bb buttons that input the smily wherever the cursor is in the text box. I have been given this code and it just takes me to the IE cannot find this page.

Please can someone tell me what im doing wrong. Its trying to link to a page on my local host named

[code]document.selection.createRange().text='::smilie::'[/code]

But obviously that isn't there as the text box is on the same page.

[code]
<td width="30" bgcolor="#565656"><a href="document.selection.createRange().text='::smilie::'"><img src="images/smilies/cool.gif"></a></td>
[/code]

??? ??? ???

Thanks
Link to comment
Share on other sites

not sure if you've solved the problem yet
try something like this
[code]
<html>
<head>
<script type="text/javascript">
function AddEmo(tag)
{
var emo=document.forms["THE id OF YOUR FORM"].elements["THE ID OF YOUR TEXT AREA"];
emo.value+="::"
emo.value+=tag
emo.value+="::"
}
</script>
</head>
<body>
<img src="http://franklyn.ifastnet.com/News/emoticons/wink.png" onClick="AddEmo('wink')">
</body>
</html>
[/code]
if you would like to use the same button on other form elements just pass the form id along with the emoticon name.
Link to comment
Share on other sites

I still can't get thi sto work.  Can you show me whic bits I need to change and exactly how it shoudl be written. I am using the following

[code]<script type="text/javascript">

function insertAtCursor(myField, myValue) {
  //IE support
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == ‘0′) {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
                  + myValue
                  + myField.value.substring(endPos, myField.value.length);
  } else {
    myField.value += myValue;
  }
}
</script>[/code]

and then

[code]<td width="30" bgcolor="#565656"><img src="images/smilies/smile.gif" onCLick="imagesinsertAtCursor(edit_post.html.edit_form.edit_area, ‘::smile::’");></td>[/code]

Sorry I really am shocking with Javascript. PHP comes easy to me but for some reason I just can't get the grasp of Javascript.
Link to comment
Share on other sites

here check this out . it took me a while to figure out how it works
[code]
<html>
<head>
<script type="text/javascript">
function AddEmo(tag)
{
var emo=document.forms["frm1"].elements["post"];
if (document.selection)
{
emo.focus();
var sel = document.selection.createRange();
sel.text = tag
}
else if (emo.selectionStart || emo.selectionStart == '0')
{

    var startPos = emo.selectionStart
    var endPos = emo.selectionEnd
    emo.value = emo.value.substring(0, startPos)
                  + tag
                  + emo.value.substring(endPos, emo.value.length)
}
else {
    emo.value += tag;
  }
}
// calling the function
</script>
</head>
<body>
<form method="post" name="frm1" id="frm1">
<img src="http://franklyn.ifastnet.com/News/emoticons/smile.png" onclick="AddEmo('::smile')">
<input type="text" name="post" id="post">
</form>
</body>
</html>
[/code]
Link to comment
Share on other sites

Its me again. I know im probably really pushing my luck here but have you any idea if this function can be adapted to wrap around  tags ie place [b] [/b] in fromt of and at the end of highlighted text. If you can point me in the direction of where to look for this i will try and work it out.

Thanks
Link to comment
Share on other sites

well i did a fair amount of gooigling and coding and this is what i came up with. it only works in IE for now FF doesn't have support for selections within textareas.
[code]
<html>
<head>
<script type="text/javascript">
function AddEmo(tag)
{
var emo=document.forms["frm1"].elements["post"];
if (document.selection)
{
emo.focus();
var sel = document.selection.createRange();
sel.text = tag
}
else if (emo.selectionStart || emo.selectionStart == '0')
{

    var startPos = emo.selectionStart
    var endPos = emo.selectionEnd
    emo.value = emo.value.substring(0, startPos)
                  + tag
                  + emo.value.substring(endPos, emo.value.length)
}
else {
    emo.value += tag;
  }
}
function AddTag(tag)
{
var txt = '';
if (window.getSelection)
{
txt = window.getSelection();

}
else if (document.getSelection)
{
txt = document.getSelection();
}
else if (document.selection)
{
txt = document.selection.createRange().text;
}
var stri1 = txt
var stri = "[tag]"+stri1+"[/tag]"

AddEmo(stri);
}
// calling the function
</script>
</head>
<body>
<form method="post" name="frm1" id="frm1">
<img src="http://franklyn.ifastnet.com/News/emoticons/smile.png" onclick="AddEmo('::smile::')">
<img src="http://musicbrainz.org/images/mblookup-tag.gif" onclick="AddTag('[img]')">
<br>
<textarea name="post" id="post">
</textarea>
</form>
</body>
</html>
[/code]
Link to comment
Share on other sites

My Mistake. All working now.

You deserve a mention somewhere on my site. Not related to coding or owt but I think i will put javascript thanks to franklyn in there coz you don't give me that much help for nowt. let me know some link details etc and i'll get it added in for you.


Ta
Link to comment
Share on other sites

Look Im really sorry to bug you on this one again but I have added the code as you suggested. 

Everything works fine except it is not picking up the tag variable

ie

The line in my code reads

[code]<img src="images/smilies/link.jpg" onclick="AddTag('[url]')">&nbsp;&nbsp[/code]

Now unless im mistaken this should print

[url][/url] into the txt box.

What it is actually printing is

[tag][/tag]

DO I need to make a different function for every different type of tag I use.

Thanks
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.