Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > newbie question: trouble with document.selection.createRange();

Reply
Thread Tools

newbie question: trouble with document.selection.createRange();

 
 
lawrence
Guest
Posts: n/a
 
      05-01-2004
Below you'll see some code that I have in one of my forms. I was
hoping to have these buttons and when I click on them they would take
selected text from a textarea box and replace it with the text but
surrounded with the HTML tags I wanted. But I can't get this to work.
Why?









<script language="javascript">
function wrapSelectionBold (element) {
var range = document.selection.createRange();
if (range.parentElement() == element)
range.text = '<b>' + range.text + '<\/b>';
}
function wrapSelectionItalic (element) {
var range = document.selection.createRange();
if (range.parentElement() == element)
range.text = '<i>' + range.text + '<\/i>';
}
function wrapSelectionBlockQuote (element) {
var range = document.selection.createRange();
if (range.parentElement() == element)
range.text = '<blockquote>' + range.text + '<\/blockquote>';
}
function wrapSelectionBigHeadline (element) {
var range = document.selection.createRange();
if (range.parentElement() == element)
range.text = '<h1>' + range.text + '<\/h1>';
}
function wrapSelectionSmallHeadline (element) {
var range = document.selection.createRange();
if (range.parentElement() == element)
range.text = '<h3>' + range.text + '<\/h3>';
}
</script>


<input type="button" value="bold"
onclick="wrapSelectionBold(this.form.inputId2)" />
<input type="button" value="italic"
onclick="wrapSelectionItalic(this.form.inputId2)"/>
<input type="button" value="blockquote"
onclick="wrapSelectionBlockQuote(this.form.inputId 2)"/>
<input type="button" value="big headline"
onclick="wrapSelectionBigHeadline(this.form.inputI d2)"/>
<input type="button" value="small headline"
onclick="wrapSelectionSmallHeadline(this.form.inpu tId2)"/>







<div class="formElement">
Change the brief description or introduction for your Weblog; <br>
Or change the contents of your Webpage:<br>
<textarea id="inputId2" name="formInputs[cbMainContent]"
class="textareaInput"> </textarea>
<p>HTML into symbols? <input type="checkbox"
name="formInputs[usingHtml]" value="y" class="textareaCheckbox"></p>

</div>
 
Reply With Quote
 
 
 
 
lawrence
Guest
Posts: n/a
 
      05-01-2004
Mostly I'm wondering if "this.form.inputId2" is the right syntax, or
does it need to be fuller, something with
document.form.element(id).value, etc. What is the best way to go?


http://www.velocityreviews.com/forums/(E-Mail Removed) (lawrence) wrote in message news:<(E-Mail Removed). com>...
> Below you'll see some code that I have in one of my forms. I was
> hoping to have these buttons and when I click on them they would take
> selected text from a textarea box and replace it with the text but
> surrounded with the HTML tags I wanted. But I can't get this to work.
> Why?
>
>
>
>
>
> <script language="javascript">
> function wrapSelectionBold (element) {
> var range = document.selection.createRange();
> if (range.parentElement() == element)
> range.text = '<b>' + range.text + '<\/b>';
> }
> function wrapSelectionItalic (element) {
> var range = document.selection.createRange();
> if (range.parentElement() == element)
> range.text = '<i>' + range.text + '<\/i>';
> }
> function wrapSelectionBlockQuote (element) {
> var range = document.selection.createRange();
> if (range.parentElement() == element)
> range.text = '<blockquote>' + range.text + '<\/blockquote>';
> }
> function wrapSelectionBigHeadline (element) {
> var range = document.selection.createRange();
> if (range.parentElement() == element)
> range.text = '<h1>' + range.text + '<\/h1>';
> }
> function wrapSelectionSmallHeadline (element) {
> var range = document.selection.createRange();
> if (range.parentElement() == element)
> range.text = '<h3>' + range.text + '<\/h3>';
> }
> </script>
>
>
> <input type="button" value="bold"
> onclick="wrapSelectionBold(this.form.inputId2)" />
> <input type="button" value="italic"
> onclick="wrapSelectionItalic(this.form.inputId2)"/>
> <input type="button" value="blockquote"
> onclick="wrapSelectionBlockQuote(this.form.inputId 2)"/>
> <input type="button" value="big headline"
> onclick="wrapSelectionBigHeadline(this.form.inputI d2)"/>
> <input type="button" value="small headline"
> onclick="wrapSelectionSmallHeadline(this.form.inpu tId2)"/>
>
>
>
> <div class="formElement">
> Change the brief description or introduction for your Weblog; <br>
> Or change the contents of your Webpage:<br>
> <textarea id="inputId2" name="formInputs[cbMainContent]"
> class="textareaInput"> </textarea>
> <p>HTML into symbols? <input type="checkbox"
> name="formInputs[usingHtml]" value="y" class="textareaCheckbox"></p>
>
> </div>

 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      05-03-2004
On 1 May 2004 12:56:35 -0700, lawrence <(E-Mail Removed)> wrote:

> Mostly I'm wondering if "this.form.inputId2" is the right syntax, or
> does it need to be fuller, something with
> document.form.element(id).value, etc. What is the best way to go?


Your original question has been answered in the latter version of this
thread. As for the question above, it is best if you use the DOM
collections to access controls that utilise id attributes:

this.form.elements[ 'inputId2' ]

To access a form that uses an id attribute, use

document.forms[ 'formID' ]

Hope that helps,
Mike

--
Michael Winter
(E-Mail Removed)d (replace ".invalid" with ".uk" to reply)
 
Reply With Quote
 
lawrence
Guest
Posts: n/a
 
      05-04-2004
Michael Winter <(E-Mail Removed)> wrote in message
> Your original question has been answered in the latter version of this
> thread. As for the question above, it is best if you use the DOM
> collections to access controls that utilise id attributes:
>
> this.form.elements[ 'inputId2' ]
>
> To access a form that uses an id attribute, use
>
> document.forms[ 'formID' ]
>
> Hope that helps,
> Mike


Thanks. And if I have only one form on the page, then these two work
about the same?

document.forms[0].elements[ 'inputId2' ]

document.forms[ 'formID' ]
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      05-04-2004
On 4 May 2004 09:49:21 -0700, lawrence <(E-Mail Removed)> wrote:

[snip]

> Thanks. And if I have only one form on the page, then these two work
> about the same?
>
> document.forms[0].elements[ 'inputId2' ]
>
> document.forms[ 'formID' ]


Yes. You can use either of the collections (forms and elements) with
indicies, or strings that contain the name or id of the form/control. So
with this HTML snippet:

<body>
...
<form id="myForm">
...
</form>

Both of these expressions return a reference to the same control:

document.forms[ 0 ]
document.forms[ 'myForm' ]

Hope that helps,
Mike

--
Michael Winter
(E-Mail Removed)d (replace ".invalid" with ".uk" to reply)
 
Reply With Quote
 
lawrence
Guest
Posts: n/a
 
      05-06-2004
Since one can't grab a selection in Netscape (there is no range() ) is
there a way to find out where the cursor is in a text box and insert
text there?


Michael Winter <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> On 4 May 2004 09:49:21 -0700, lawrence <(E-Mail Removed)> wrote:
>
> [snip]
>
> > Thanks. And if I have only one form on the page, then these two work
> > about the same?
> >
> > document.forms[0].elements[ 'inputId2' ]
> >
> > document.forms[ 'formID' ]

>
> Yes. You can use either of the collections (forms and elements) with
> indicies, or strings that contain the name or id of the form/control. So
> with this HTML snippet:
>
> <body>
> ...
> <form id="myForm">
> ...
> </form>
>
> Both of these expressions return a reference to the same control:
>
> document.forms[ 0 ]
> document.forms[ 'myForm' ]
>
> Hope that helps,
> Mike

 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      05-06-2004
On 5 May 2004 17:56:17 -0700, lawrence <(E-Mail Removed)> wrote:

> Since one can't grab a selection in Netscape (there is no range() ) is
> there a way to find out where the cursor is in a text box and insert
> text there?


Not that I know of (that doesn't necessarily mean there isn't a way).

[snipped top-post]

Mike

--
Michael Winter
(E-Mail Removed)d (replace ".invalid" with ".uk" to reply)
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      05-19-2004
lawrence wrote:

> Since one can't grab a selection in Netscape (there is no range() )


There is in versions 6 and above, but it is named different.

> is there a way to find out where the cursor is in a text box and
> insert text there? [...]


There is. This is a FAQ, please search before you post.

> [Top post]


And please do not do that, see the FAQ, too.


PointedEars
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i have no trouble to send , ihave trouble reciving mail --any ideas John Penney Computer Support 4 08-29-2006 08:45 PM
newbie having trouble with control structures: loop till recieve string QUIT M. Duijkers Perl 2 12-12-2004 11:02 AM
Newbie: Trouble w/ ViewState =?Utf-8?B?Y2xzbWl0aDY2?= ASP .Net 1 12-07-2004 10:25 PM
pix 501 setup trouble newbie Greg Gibson Cisco 13 11-28-2003 04:45 PM
trouble with caching or caching the trouble Hypo ASP .Net 6 08-01-2003 07:11 AM



Advertisments