Ben Rogers
04-16-2004, 07:10 PM
Right, I'm totally lost on this one. I'm making a posting script (with PHP, which I actually get to some degree) and I'm trying to make a javascript to insert text into the textare (just like the one used by this forum). Now, I could just rip off the script used here, but not only would that be plagiarism, I'm trying to learn a bit of complex javascript. I may've been overambitious with this, and I also may've applied PHP syntax/ rules/ funtions that JS didn't have... I hope not, but I did my best, and nothing is happening at all... so I'm stuck.
JS: <script type="text/javascript">
<!-- Begin Javascript
//- Declare variables and arrays
var txt = document.post.txt.value;
var insertWhat = new Array("subtopic", "bold", "italic", "underlined", "link");
var insertMeBegin = new Array("[subtop]", "[b]", "[i]", "[u]");
var insertMeEnd = new Array("[/subtop]", "[/b]", "[/i]", "[/u]");
//- Function which makes prompt appear, checks legnth of insertWhat, then for each one,
//- if it is the same as what, it'll insert text in the textarea, then break the loop
function insert(what) {
var input = prompt("Type in the text you want to be"+input+".", "");
for(n=0; n < insertWhat.legnth, n++) {
if(what == insertWhat[n]) {
txt += insertMeBegin[n]+input+insertMeEnd[n];
break;
} else {
return;
}
}
}
// End Javascript -->
</script>
and the form's html <form action="/index.php?id=post" method="post" name="post">
<div>
<!-- Javascript powered buttons to activate functions to insert "keytext" to be parsed -->
<h3>Insert: </h3>
<input type="button" value="Sub topic" onclick="insert('subtopic')" />
<input type="button" value="Bold text" onclick="insert('bold')" />
<input type="button" value="Italic text" onclick="insert('italic')" />
<input type="button" value="Underlined text" onclick="insert('underlined')" />
<input type="button" value="Link" onclick="insert('link')" />
<!-- Actual form, topic for <h2></h2>, textarea for post -->
<textarea name="txt"></textarea>
</div>
</form>
EDIT: I tried changing the brackets to their respective ascii entities so it wouldn't mess up the post...
JS: <script type="text/javascript">
<!-- Begin Javascript
//- Declare variables and arrays
var txt = document.post.txt.value;
var insertWhat = new Array("subtopic", "bold", "italic", "underlined", "link");
var insertMeBegin = new Array("[subtop]", "[b]", "[i]", "[u]");
var insertMeEnd = new Array("[/subtop]", "[/b]", "[/i]", "[/u]");
//- Function which makes prompt appear, checks legnth of insertWhat, then for each one,
//- if it is the same as what, it'll insert text in the textarea, then break the loop
function insert(what) {
var input = prompt("Type in the text you want to be"+input+".", "");
for(n=0; n < insertWhat.legnth, n++) {
if(what == insertWhat[n]) {
txt += insertMeBegin[n]+input+insertMeEnd[n];
break;
} else {
return;
}
}
}
// End Javascript -->
</script>
and the form's html <form action="/index.php?id=post" method="post" name="post">
<div>
<!-- Javascript powered buttons to activate functions to insert "keytext" to be parsed -->
<h3>Insert: </h3>
<input type="button" value="Sub topic" onclick="insert('subtopic')" />
<input type="button" value="Bold text" onclick="insert('bold')" />
<input type="button" value="Italic text" onclick="insert('italic')" />
<input type="button" value="Underlined text" onclick="insert('underlined')" />
<input type="button" value="Link" onclick="insert('link')" />
<!-- Actual form, topic for <h2></h2>, textarea for post -->
<textarea name="txt"></textarea>
</div>
</form>
EDIT: I tried changing the brackets to their respective ascii entities so it wouldn't mess up the post...