...

View Full Version : Insert BB with onchange



stophon4
04-29-2004, 10:28 PM
I have some BB code that I am inserting with JavaScript but it isn't working...
I have this code but it comes up "undefined"



<SELECT Onchange='Javascript: document.post.comments.value += " "'>
<option Value="default">Default</option>
<option Value="red" style="font-color: red">Red</option>
</SELECT>

sad69
04-29-2004, 10:58 PM
Try one of the following (instead of this.SelectedIndex.value):


this.value


or


this.options[this.selectedIndex].value


Let me know how that works out for you.
Sadiq.

Willy Duitt
04-29-2004, 11:23 PM
The value Red is not a size.
Are you looking for the index which would be 1 if Red was chosen?
If so, remove the value.

eg: this.options[this.selectedIndex]

.....Willy

stophon4
04-29-2004, 11:37 PM
sad69's idea worked. Yeah red is not a size. That was a stupid color/font screw up. And it works just like this forum :thumbsup:

www.hostultra.com/~stophon4/guestbook/gb.php

sad69
04-29-2004, 11:43 PM
Sort of.. couple of bugs on that guest book, but pretty neat still!

Main thing for me is that if you select some text and press on of the buttons, it doesn't recognize that you've selected anything and expects new inputs.. but that's not a huge deal I suppose..

Also, when using the buttons (like bold or whatever) and pressing cancel results in a null. But you might already know about that..

Pretty cool though, good job!

Sadiq.

stophon4
04-30-2004, 02:24 PM
Thanks, yeah I think I know how to stop the null thing from happening, but I don't know how to do the highlight thing, what function reonizes what text is highlighted?

Anyway the guestbook works great except for that stuff :thumbsup:
Well at least in JavaScript way, there is this php bug that makes it so whenever you put a ' or " it puts a slash in there :confused:

EDIT:
Well I thought I new what to do for the null thing, but it still comes up null and nothing changes...


function Bold()
{
var bold =prompt("Please enter the text you wish to appear in bold:", "")
if(bold == 'null')
{
document.post.comments.value += "";
}else{
document.post.comments.value += "" + bold + ""
}
}

sad69
04-30-2004, 05:31 PM
Maybe remove the quotes around null? ie. 'null' => null

As for detecting the selection, check out this link:
http://www.faqts.com/knowledge_base/view.phtml/aid/18053/fid/145

As for the ' " problem in php, try using the stripslashes() method when echoing out a guestbook entry.

Hope that helps,
Sadiq.

sad69
04-30-2004, 05:42 PM
Actually I was just re-reading that link and there's a typo: the r should be a w.

I've altered it slightly to show you how to stick in whatever you want (bolding in my example):


<html>
<head>
<script>
function formatSelected(from_obj, formatting) {
from_obj.focus ();
var w = self.document.selection.createRange ();
w.text = "["+formatting+"]"+w.text+"[/"+formatting+"]";
}
</script>
</head>
<body>
<textarea id = "forSELECT" cols = "30" rows = "10">some text some text some text...</textarea>
<p>
<input type = "button" value = "View selected" onclick = "formatSelected(forSELECT, 'b');">
</p>
</body>
</html>

Hope that helps,
Sadiq.

stophon4
04-30-2004, 07:09 PM
Actually I was just re-reading that link and there's a typo: the r should be a w.

I've altered it slightly to show you how to stick in whatever you want (bolding in my example):


<html>
<head>
<script>
function formatSelected(from_obj, formatting) {
from_obj.focus ();
var w = self.document.selection.createRange ();
w.text = "["+formatting+"]"+w.text+"[/"+formatting+"]";
}
</script>
</head>
<body>
<textarea id = "forSELECT" cols = "30" rows = "10">some text some text some text...</textarea>
<p>
<input type = "button" value = "View selected" onclick = "formatSelected(forSELECT, 'b');">
</p>
</body>
</html>

Hope that helps,
Sadiq.

KK, I got the null thing working and also the stripslashes() thing working, but I don't understand how you did the bold highlight (above) maybe you should explain it a little bit more, sorry for not understanding :p

sad69
04-30-2004, 07:32 PM
Ok.. which part don't you understand?

There's a button, right? Do you see the onClick event on the button? It calls the function (formatSelected) that inserts some formatting around the selected text.

The function formatSelected takes two parameters: the textarea and the formatting you want (I pass 'b' as I want [ b] .. [ /b] to show up). The function then grabs the selected text, and then I insert the [, the formatter and ], the selected text, the [/, the formatter, and the ]. And that's it.

If you're still lost, let me know what part you don't understand..

Sadiq.

stophon4
04-30-2004, 08:24 PM
:confused: :confused: :confused: :confused: :confused: :confused:

Maybe you should explain each part of the code, and also is w.text the highlighted variable?

sad69
04-30-2004, 08:39 PM
is w.text the highlighted variable?

Yep.

I just explained the entire code.

Why don't you break down the code that you DO understand, and the code that you DON'T understand.

Sadiq.

stophon4
04-30-2004, 09:04 PM
OK, I got alot farther this time, but the text box says [object], not what I have highlighted, here is the code:


function Bold(from_obj, formatting)
{
from_obj.focus ();
var w = self.document.selection.createRange ();
var bold =prompt("Please enter the text you wish to appear in bold:", "" + w +"")
if(bold == null)
{
document.post.comments.value += "";
}else{
document.post.comments.value += "" + bold + ""
}
}

sad69
04-30-2004, 09:53 PM
var bold =prompt("Please enter the text you wish to appear in bold:", "" + w +"")


should read:


var bold = prompt("Please enter the text you wish to appear in bold:", "" + w.text +"");


Let me know how that works out for you.

Sadiq.

stophon4
04-30-2004, 10:32 PM
var bold =prompt("Please enter the text you wish to appear in bold:", "" + w +"")


should read:


var bold = prompt("Please enter the text you wish to appear in bold:", "" + w.text +"");


Let me know how that works out for you.

Sadiq.
You can see for yourself what it does :confused:

sad69
04-30-2004, 10:44 PM
function Bold(from_obj, formatting) {
from_obj.focus();
var w = self.document.selection.createRange();
var bold = prompt("Please enter the text you wish to appear in bold:", "" + w.text +"")
if(bold != null) {
w.text = "["+formatting+"]"+bold+"[/"+formatting+"]";
}
}


I think that should do what you're trying for..

Sadiq.

stophon4
05-01-2004, 01:32 AM
kk
it's updated, you can see the result :o



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum