Jump to content

Recommended Posts

Hi there. I have a problem with my javascript. I want to insert a symbol into a textarea when the person clicks on the link (just like the smiley selection in this forum). I then need the cursor to focus back to where they were when they clicked so they can carry on typing as normal. The current code I have has such a varied response across the browsers that I am just confused. When used in Firefox or Opera, it will insert the symbol to where the cursor was, but will then not focus back there and if the user clicks again will insert the symbol at the end. However, with Safari and Chrome they are slightly different. They will insert the symbol to where the cursor was and will not focus back there, but if the user clicks again will insert the symbol before the last one. Then theres IE (:facewall:). If the user clicks to insert the symbol it will insert it at the beginning only and will not refocus.

 

Any help will be much appreciated.

Joe

 

 

The js code.

function insertSymbol(id,td)
{
	var symbol = document.getElementById(id).innerHTML
	var textAreaContent = document.getElementById(td)
	var first = textAreaContent.value.slice(0, textAreaContent.selectionStart)
	var second = textAreaContent.value.slice(textAreaContent.selectionStart)
	textAreaContent.value = first + symbol + second
	textAreaContent.focus(selectionStart) 

// Removing 'selectionStart' from the focus function will let it refocus at the end of the text in IE and FF. Will refocus in front of the newly inputted symbol in Safari and Chrome. Will not work at all in Opera.

}

 

The html code.

onClick="insertSymbol('##', 'textArea')"

 

I've got a similar issue. After lots of digging I'm about to give up. Basically it comes down to finding the correct cursor position in the string within the textarea... after lots of reading it seems very difficult.

 

 

Does anyone know a jquery way of doing this?

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.