This is my first time writing in JS and I am having some trouble. This code was put together with a bunch of different codes. It is going to be used as part of a larger code when it is finished.
Right now, this code does nothing, I click the button and no alerts come up.
Code:
script language="JavaScript" type="text/javascript">
<!--
function checkSelection() {
//IE
var field = document.post.testfield;
if (document.all) {
//Text is Selected
if (strSelection!="") {
alert ("IE: Text is Selected")
}
//Text is not Selected
else {
alert ("IE: Text is not Selected")
}
}
//FF
else if (document.getElementById) {
//Text is Selected
if(field.selectionStart - field.selectionEnd != 0){
alert ("Firefox: Text is Selected")
}
//Text is not Selected
else {
alert ("Firefox: Text is not Selected")
}
}
}
//-->
</script>
<form action="posting.php" method="post" name="post">
<textarea name="testfield">
Some sample teext for testing.
</textarea>
<input type="button" value="Test" onclick="checkSelection();">
</form>
I have seen those scripts before and mozedit was a major resource in developing the piece of code I already made, except it is not working.
You can see the similarities between these three functions and my code. I have something different to do with the text if it is not selected, so I attempted to make an if statement to check if it is selected or not. Except it doesn't work, thats why I posted this. Do you see any problems in my code?
Code:
function mozWrap(txtarea, lft, rgt) {
var selLength = txtarea.textLength;
var selStart = txtarea.selectionStart;
var selEnd = txtarea.selectionEnd;
if (selEnd==1 || selEnd==2) selEnd=selLength;
var s1 = (txtarea.value).substring(0,selStart);
var s2 = (txtarea.value).substring(selStart, selEnd)
var s3 = (txtarea.value).substring(selEnd, selLength);
txtarea.value = s1 + lft + s2 + rgt + s3;
}
function IEWrap(lft, rgt) {
strSelection = document.selection.createRange().text;
if (strSelection!="") {
document.selection.createRange().text = lft + strSelection + rgt;
}
}
function wrapSelection(txtarea, lft, rgt) {
if (document.all) {IEWrap(lft, rgt);}
else if (document.getElementById) {mozWrap(txtarea, lft, rgt);}
}
Edit: I started writing this before you figured out why it didn't work in IE/Win.
In your script you didn't set "strSelection", so no, it won't work in IE/Win.
It works in Firefox though. It won't work in Opera because opera supports document.all but not IE's method of doing this. Opera 8+ supports Firefox's (Mozilla's) way of doing this.
Just because one of the scripts uses object detection in an idiotic way doesn't mean you should copy it.
It should look more like this:
Code:
<script type="text/javascript"><!--
function checkSelection() {
//IE/Win
var field = document.post.testfield;
if (document.selection && document.selection.createRange) {
//Text is Selected
strSelection = document.selection.createRange().text;
if (strSelection!="") {
alert ("IE: Text is Selected");
}
//Text is not Selected
else {
alert ("IE: Text is not Selected");
}
}
//FF and Opera 8+
else if (field.selectionStart || field.selectionStart == '0') {
//Text is Selected
if(field.selectionStart - field.selectionEnd != 0){
alert ("Firefox: Text is Selected");
}
//Text is not Selected
else {
alert ("Firefox: Text is not Selected");
}
}
}
// -->
</script>
I thought something might be messed up in my browser detection, but then I had it working in FF and IE so I figured it was OK. Thanks for that info. Is all that you changed the 2 if statements?
I just added in the next big chunk of code, and it is not working again. Are all of my firefox functions going to work in Opera too?
My final result is going to be a bbCode editor. I realize there are some out there already, but none that work exactly how I need them to and I would like to learn some javascript. Thanks for all you're help so far, I thought I would be able to get past this step without any help, but no such luck.
Here is my new code:
Code:
<script language="JavaScript" type="text/javascript">
<!--
function addBB(type) {
//IE and Win
var field = document.post.testfield;
if (document.selection && document.selection.createRange) {
//Text is Selected
strSelection = document.selection.createRange().text;
if (strSelection!="") {
if (type == b || type == i || type == u){
var lft = '[' + type + ']';
var rgt = '[/' + type + ']';
IEWrap(lft, rgt);
}
}
//Text is not Selected
else {
if (type == b || type == i || type == u){
var txt = prompt('Enter the text you would like the styple applied to', '');
var new = '[' + type + ']' + txt + '[/' + type + ']';
IECursor(field, new);
}
}
}
//FF and Opera
else if (field.selectionStart || field.selectionStart == '0') {
//Text is Selected
if(field.selectionStart - field.selectionEnd != 0){
if (type == b || type == i || type == u){
var lft = '[' + type + ']';
var rgt = '[/' + type + ']';
MozWrap(field, lft, rgt);
}
}
//Text is not Selected
else {
if (type == b || type == i || type == u){
var txt = prompt('Enter the text you would like the styple applied to', '');
var new = '[' + type + ']' + txt + '[/' + type + ']';
MozCursor(field, new);
}
}
}
}
function IECursor(field, value) {
field.focus();
sel = document.selection.createRange();
sel.text = value;
}
function IEWrap(lft, rgt) {
document.selection.createRange().text = lft + strSelection + rgt;
}
function MozCursor(myField, myValue) {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
}
function MozWrap(txtarea, lft, rgt) {
var selLength = txtarea.textLength;
var selStart = txtarea.selectionStart;
var selEnd = txtarea.selectionEnd;
var s1 = (txtarea.value).substring(0,selStart);
var s2 = (txtarea.value).substring(selStart, selEnd)
var s3 = (txtarea.value).substring(selEnd, selLength);
txtarea.value = s1 + lft + s2 + rgt + s3;
}
//-->
</script>
<form action="posting.php" method="post" name="post">
<textarea name="testfield">
Some sample teext for testing.
</textarea>
<img src="asdf.jpg" onclick="addBB('b');">
</form>