Add contect to textfield using buttons

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Add contect to textfield using buttons

Post by deejay »

Hi

At present I have created a simple webpage that has a form so I can update a blog from my mobile.

I would like to couple of buttons that would add content to the textarea , a bit like http://tinymce.moxiecode.com/ but as this page would be accessed on mobiles I'd like to keep it real simple.

so far I have

Code: Select all

 
<script type="text/javascript" language="javascript">
 
function MkeBold()
{
   var MyElement = document.getElementById("introtext");
   MyElement.value = "<b></b>";
 
   return true;
}
 
function AddImg()
{
   var MyElement = document.getElementById("introtext");
   MyElement.value = "<img src=\"http://www.wavelengthmag.co.uk/images/A ... GENAMEHERE\">";
 
   return true;
}
 
</script>
 

Code: Select all

 
<img src="images/buttons/bold.gif" alt="BOLD" onClick="MkeBold()" /> 
                                    <TEXTAREA WRAP=PHYSICAL NAME="introtext" ROWS=4 COLS=25 id="introtext"></TEXTAREA>
the problem with this is that it'll write over everything that's in the box. Can anyone push me in the right direction for a better solution.

Thanks in advance
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Add contect to textfield using buttons

Post by pickle »

There is a way (not sure exactly how) to determine where the cursor position is in a textfield, then insert text at that position.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Add contect to textfield using buttons

Post by kaszu »

It's almost the same as what editor on phpBB forums does except instead of BB code you have html, check 'insert_text' function in styles/prosilver/template/editor.js
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: Add contect to textfield using buttons

Post by deejay »

Cheers guys

I had a quick search for 'cursor position is in a textfield' last night, but it seems pretty browser specific; which puts me off when we'll be looking to access this page from mobile devices.

kaszu :

I don't suppose you've got an example of the call up for this funtion. that way I could look at using it without having to understand how it works. Am hoping to get a spare moment at sometime to have a closer look at it.

Think I'm going to shelf this for a moment and maybe think of an easier solution.
Post Reply