PDA

View Full Version : [Javascript] Input into textarea with alerts



R4g1ng
August 2nd, 2005, 10:16
You know that on here, when you click like the underline button, for example; an alert pops up asking what text you want to be underlined and then it puts it in the textarea with the bbcode tags. I'd like to do the same for my blog script but I don't know javascript at all. Can anyone help me? I know that its not limited to pre-scripted forums because I've seen a hand-coded one with the same code.
PS. If possible, I'd also like to know how to do the same with the url tags.

josku_x
August 2nd, 2005, 10:49
Well, in JavaScript you can't do "BB" code shortcuts, it is made in PHP and it is hard for a starter.

But I can help you to have an alertbox with a place to write something.


<HTML>
<SCRIPT language="JavaScript">
var string = prompt('Here the text or question.', 'here pre-entered text. '); //Initialize a new prompt

if ( (string==' ') || (string==null) ) //initializes if the string entered was empty
{
string="";
}
document.write(string) //Writes the string
</SCRIPT>
</HTML>
Hope you get the idea of what all is about.

R4g1ng
August 2nd, 2005, 11:00
I've got the BBCode, just need the code for the alert part.
Sorry if i sound stupid but I don't know that much Javascript. Where do I put the script and which parts do i need to edit? I mean, where do I define where to put the text?

ryza
August 2nd, 2005, 13:37
Heres a more complete example,

Just modify your <FORM> so it has the OnSubmit part.
onSubmit="javascript:return doCheck(field1.value,field2.value)"

and add whatever fields u want in the javascript area.





<script language="javascript">
<!--
function doCheck(field1,field2){
if (field1 == "" || field1 == "ryza"){
alert("Please enter field 1!");
return false;
}
if (field2 == "" || field2 == "ryza"){
alert("Please enter field 2!");
return false;
}
}
//-->
</script>

<form onSubmit="javascript:return doCheck(field1.value,field2.value)">
<INPUT TYPE="text" NAME="field1" value="">
<INPUT TYPE="text" NAME="field2" value="">
<INPUT TYPE="submit">
</form>

R4g1ng
August 3rd, 2005, 07:05
Umm..ryza, I don't think you understood my post properly. The code you posted validates fields, I could do that in PHP. What I want is the code for the alert boxes that puts the info inserted into the textarea, like on IPB and vBulletin (here).

EDIT: wait...I think I got it, except where do I define where to write what's inputted?

ryza
August 3rd, 2005, 10:43
ahh ok, sorry i understand now.

josku_x
August 3rd, 2005, 11:13
R4g1ng, look at my code, properly, use it, have fun, no need to credit me.

R4g1ng
August 3rd, 2005, 11:21
Okay, thanks. I'm sure if I look up a few more examples I'll figure it out. ^^

josku_x
August 3rd, 2005, 11:33
My previous code was still a little confusing. I made new code with a textarea, where the inputted string will be shown.

<HTML>
<SCRIPT language="JavaScript">
var string = prompt('Here the text or question.', '');

if ( (string==' ') || (string==null) )
{
string="";
}
document.write('<FORM>');
document.write('<textarea rows="9" name="test" cols="34">');
document.write(string);
document.write('</textarea>');
document.write('</FORM>');
</SCRIPT>
</HTML>

PS: I made some text red, you should only edit those unless you know what you are doing :P