Re: how to make selected text bold but only in one specific textbox of the form?

Tech-Archive recommends: Fix windows errors by optimizing your registry



ASP.NET IDs are not necessarily the same as the HTML DOM ID.
You can either check the view-source or use something like:

var sTargetId = "<% TextBox1.ClientID %>";

enclosed in a client-side script element to place the ID on the page and make it available to your code.

Martin intentionally used '===' rather than '==' as the extra equals sign means that no type conversion is used in the comparison.

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

"Bob" <sdfdl@xxxxxxxxxxx> wrote in message news:#R5HyzevJHA.1536@xxxxxxxxxxxxxxxxxxxxxxx
My aspx file contains this:

<asp:TextBox ID="titel" runat="server" ></asp:TextBox>
<asp:TextBox ID="TextBox1" runat="server" asp:TextBox>

I then tried this: alert(range.parentElement().id)
and now, i always get 'titel' even if the selected text is in TextBox1



"Martin Honnen" <mahotrash@xxxxxxxx> schreef in bericht news:e%23mEPZCvJHA.4364@xxxxxxxxxxxxxxxxxxxxxxx
Bob wrote:
Hi,

I have several textboxes in my aspx file (asp.net).
I want to make the selected text bold but this may only happen in "TextBox1", not into the others.
I work only with IE7.

This code works, but it also works in all textboxes.

var field = document.getElementById("TextBox1");
var seltxtb = "<b>"+document.selection.createRange().text+"</b>";
var emo = ""

emo = window.event.srcElement.id
if (emo == "b" && document.selection.createRange().text != "")
{ document.selection.createRange().text = seltxtb; }

How can i limit this only to TexyBox1 ?

I think you want e.g.
var field = document.getElementById("TextBox1");
var range = document.selection.createRange();
if (range.parentElement() === field)
{
// now replace text here
}
That checks whether the TextRange created from the current selection is contained in that "TextBox1" element.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/





.



Relevant Pages