473,468 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

pasteHTML and font size

9 New Member
Hi,

In an editable div, I inserted a big letter, Rng.pasteHTML("<font size=5>A</font>");,fine. However, after that, when type in other letters, (sometimes) I got big letters with size=5.

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <script>
  3.    function Test()
  4.    {
  5.       document.getElementById("testDiv").focus()
  6.       var Rng = document.selection.createRange();
  7.       Rng.pasteHTML("<font size=5>A</font>");
  8.    }
  9. </script>
  10. <body>
  11.    <div id="testDiv" contentEditable="true"></div>
  12.    <input type="button" onclick="Test()">
  13. </body>
  14. </html>
Please let me know If anything wrong in the code.

What I tested by this code is:
Type ABC, click the button, the big A appears, press Return, put the cursor immediately after the big A, then type a letter and this letter is a big letter!

Any helps would be appreciated.
Dec 17 '08 #1
16 3681
pronerd
392 Recognized Expert Contributor
@markjavascript
That is not a bug. Just about every rich text editor will do that. I have the same problem with MS Word. If the cursor is placed next to that text it will be counted as inside the tags so it will have the style of those tags. You could try adding a space after the closing tag so that it will not treat the cursors location as inside the tag.
Dec 17 '08 #2
markjavascript
9 New Member
Thank you.
The problem is that I cannot disallow the user type a letter next to the big letter and there is no way to recover the font size since my div's font size is in px.
Dec 17 '08 #3
pronerd
392 Recognized Expert Contributor
@markjavascript
Sure you can. As I just explained you could add a space after the closing tag so the cursor would not end up beside the letter(s). Or you could set an onChange event for the field and check to see if the currsor is next to your tag. If it is then move it over a space or two.





@markjavascript
I do not understand what you mean. First off you are not setting your font size in px. You are not including any label for the attribute at all. Secondly your HTML syntax is incorrect you should have quotes around all of your attributes. Thirdly all you have to do to get the font size is to read the size attribute of the font tag.
Dec 17 '08 #4
markjavascript
9 New Member
In the div, I use STYLE="font-size:14px;" while in the javascript use ("<font size='5'>A</font> ".I include the font size in the div now:
-----------------------------
Expand|Select|Wrap|Line Numbers
  1. <html> 
  2. <script> 
  3.    function Test() 
  4.    { 
  5.       document.getElementById("testDiv").focus();
  6.       var Rng = document.selection.createRange(); 
  7.       Rng.pasteHTML("<font size='5'>A</font> "); 
  8.    } 
  9. </script> 
  10. <body> 
  11.    <div id="testDiv" contentEditable="true" STYLE="font-size:14px;"></div> 
  12.    <input type="button" onclick="Test()"> 
  13. </body> 
  14. </html>
-----------------------
Thank you for telling me the mistakes in my first code. Please check this code as well.

Adding a space is a good idea, but, after tested, I found the user can still put the cursor immediataly after the big letter or just delete the space, and then the problem still exists.
Dec 17 '08 #5
Dormilich
8,658 Recognized Expert Moderator Expert
some general remarks
- don't use <font> tags, these are deprecated quite a long time now, if you need to change the font size, use CSS
- contentEditable is not a valid attribute (looke like IE syntax), if you need to edit block text use <textarea>
Dec 17 '08 #6
markjavascript
9 New Member
Dormilich,

Thank you.
I think we cannot use css for the font size in pasteHTML("<font size='5'>A</font> ")
Yse <textarea> is very good, but it is not "rich", we cannot display subscript for example.
Dec 17 '08 #7
Dormilich
8,658 Recognized Expert Moderator Expert
@markjavascript
why?

@markjavascript
you could make an instant previev <div>, what for those users whose browser ignores the contentEditable attribute?
Dec 18 '08 #8
markjavascript
9 New Member
Thank you.

Do you think we can use css for the font size in pasteHTML("<font size='5'>A</font> ")? Anyway, I will try again.

In my website, only 1% visitors not use IE. I will let them select <textarea> or <div>.
Dec 18 '08 #9
Dormilich
8,658 Recognized Expert Moderator Expert
javascript simple css syntax:
Expand|Select|Wrap|Line Numbers
  1. // set font-size to 2em
  2. // element is your <div>
  3. element.style.fontSize = "2em";
and because I'm in the mood....
Expand|Select|Wrap|Line Numbers
  1. // making only the first letter big
  2. div:first-letter {
  3.   font-size: 200%;
  4. }
Dec 18 '08 #10
acoder
16,027 Recognized Expert Moderator MVP
You can use designMode in Firefox and other browsers by setting it to "on". This would set the whole document, so you would need to use an iframe. See Midas - MozillaZine Knowledge Base

In newer versions of Firefox (3 onwards), you can use contentEditable to apply to a single element. It should also be supported by Opera/Safari - not sure about Chrome.

To see an example of its use (using an iframe), see contentEditable.
Dec 18 '08 #11
Markus
6,050 Recognized Expert Expert
contentEditable does work in Chrome 1.0.
Dec 18 '08 #12
markjavascript
9 New Member
To all the experts above,

If my problem may not be solved, can we think another way:
a javascript function that says "all the typed in letters for the DIV will be with font-size=14px"?
Dec 18 '08 #13
Dormilich
8,658 Recognized Expert Moderator Expert
thats easy. add a new css rule
Expand|Select|Wrap|Line Numbers
  1. #id_of_div {
  2.   font-size: 14px;
  3. // you might be better with relative font sizes
  4. }
Dec 19 '08 #14
markjavascript
9 New Member
Thank you.
Can I put you code into a function setFont('14px')?
However, even I can put into the function setFont('14px'), do you think
pasteHTML("<font size='5'>A</font>");setFont('14px');
will solve my problem?
Dec 20 '08 #15
acoder
16,027 Recognized Expert Moderator MVP
You can either set elem.style.fontSize or set the class of the container. What you're attempting to avoid is expected behaviour. If you did want to type more things at the larger font size, where would you put the cursor?
Dec 23 '08 #16
markjavascript
9 New Member
Thanks a lot for all your helps.
Dec 23 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

115
by: J | last post by:
I've run CSSCheck on my style sheets and I always get a warning similar to this: "font: bold 9pt/100% sans-serif Warning: Absolute length units should not generally be used on the Web ..." ...
3
by: Simon Wigzell | last post by:
I'm using a 3rd party (htmlarea) WYSIWYG html editting tool. it works great most of the time. I have a problem where I've made my own function to create a bookmark type link. e.g. I want to turn...
9
by: Dr John Stockton | last post by:
Assuming default set-ups and considering all reasonable browsers, whatever that may mean, what should an author expect that his readers in general will see (with visual browsers) for a page with...
2
by: colinhumber | last post by:
I'm running this line of code: oTarget.pasteHTML(formattedHtmlText); where oTarget is a text range object. When formattedHtmlText is, say, "<STRONG><BUTTON...
4
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a...
7
by: Sakharam Phapale | last post by:
Hi All, How to preserve the old font properties while changing new one? I posted same question 2 months back, but I had very small time then. eg. "Shopping for" is a text in RichTextBox and...
7
by: Daniel Kaplan | last post by:
I have the item below at the top of my style sheet. And it seems that the font-szie is ignored. I know that my linked style sheet is being read, and used because if I remove the font-family line,...
5
by: Jason | last post by:
Hi, I'm developing an HTML Editor Control using VB.Net 2003 for an application that used to use the DHTML Editor Control that is no longer supported. Well, it's been fun but I've hit a wall with...
2
by: kheitmann | last post by:
OK, so I have a blog. I downloaded the "theme" from somewhere and have edited a few areas to suit my needs. There are different font themes within the page theme. Long story short, my "Text Posts"...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.