Jump to content

[SOLVED] button into textarea


chocopi

Recommended Posts

I have a little bbcode in php where is the user posts something like [b][/b] then it will be replaced with <b></b> and that works fine.
I have searched google and the first few pages of this section, but i cant find anything.
What i want is, when the user clicks the button it is inserted into the textarea so th 'b' button would insert [b][/b],
basically what this site has.

So if anyone can help, it would be greatly appreciated

~ Chocopi

Link to comment
Share on other sites

onclick="insertbold()"

 

insertbold() {

 

document.form[0].textareaname.value += "<b><b/>";

}

 

something like that might work assuming you have only one form in the page, im a bit of a javascript neebie myself so dont know if this is right give it a whirl.

Link to comment
Share on other sites

this is such a n00bish question, but i havent used javascript in years. where do i place the stuff  :-[

 

<html>
<body>
<form name="name" method="post" action="<?php $PHP_SELF ?>">
<center>
<br />
<input type="text" name="subject" id="subject" value="<?php $subject ?>" />
<br />
<br />
<br />
<textarea name="message" value="<?php $original ?>" cols="60" rows="15"></textarea>
<br />
<br />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
</center>
</body>
</html>

 

Thanks :)

 

~ Chocopi

Link to comment
Share on other sites

this would go in your html form

 

<input type="button" onclick="bold()" />

 

include a javascript page in your head

 

write the javascript function in the seperate javascript page

 

function bold() {

 

form[0].message.value += <b></b>;

}

 

tihs might work

 

althuogh you might have to find the form with document.getElememtById('form_name')

 

give it a whirl

Link to comment
Share on other sites

ok i have this now, but it does nothing  :(

 

<html>
<head>
<script>
function bold() {

document.getElememtById('message').message.value += ;
}
</script>
</head>
<body>
<form name="name" method="post" action="<?php $PHP_SELF ?>">
<center>
<br />
<input type="button" value="Bold" onclick="bold()" />
<br />
<br />
<input type="text" name="subject" id="subject" value="<?php $subject ?>" />
<br />
<br />
<br />
<textarea name="message" value="<?php $original ?>" cols="60" rows="15"></textarea>
<br />
<br />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
<br />
<br />
<a href="view.php">View Forum</a>
</center>
</body>
</html>

 

Thanks lighton,

 

~ Chocopi

Link to comment
Share on other sites

I had this same problem awhile back and I eventually found this function somewhere:

 

<script language="JavaScript" type="text/JavaScript">
        function sendText(e, text)
        {
           e.value+=text
        }
</script>

 

I can't exactly remember where I found it but oh well.

 

A way it can be used is:

 

<a href='#' onClick="sendText(document.FORMNAMEHERE.TEXTAREANAMEHERE, '[b][/b]')" >[b][/b]</a>

Link to comment
Share on other sites

Sadly I don't.  :(

 

If you find out please tell me though. ;D

 

EDIT:

Actually I just go curious and decided to look at this site's javascript. 

 

I found this:

function surroundText(text1, text2, textarea)
{
// Can a text range be created?
if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
{
	var caretPos = textarea.caretPos, temp_length = caretPos.text.length;

	caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;

	if (temp_length == 0)
	{
		caretPos.moveStart("character", -text2.length);
		caretPos.moveEnd("character", -text2.length);
		caretPos.select();
	}
	else
		textarea.focus(caretPos);
}
// Mozilla text range wrap.
else if (typeof(textarea.selectionStart) != "undefined")
{
	var begin = textarea.value.substr(0, textarea.selectionStart);
	var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
	var end = textarea.value.substr(textarea.selectionEnd);
	var newCursorPos = textarea.selectionStart;
	var scrollPos = textarea.scrollTop;

	textarea.value = begin + text1 + selection + text2 + end;

	if (textarea.setSelectionRange)
	{
		if (selection.length == 0)
			textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
		else
			textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
		textarea.focus();
	}
	textarea.scrollTop = scrollPos;
}
// Just put them on the end, then.
else
{
	textarea.value += text1 + text2;
	textarea.focus(textarea.value.length - 1);
}
}

 

That's what the use for their bold and image tags.  Used the same way as the other function. 

 

Example:

<a href="#" onclick="surroundText('[img=', ']', document.forms.FORMNAMEHERE.TEXTAREANAMEHERE); return false;">

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.