Solved

Javascript: How can i apply font size in pixels?

Posted on 2009-07-07
2
407 Views
Last Modified: 2012-05-07
I'm creating a text editor and making use of the contentEditable property on a <div>. My function to specify the font currently using:

document.execCommand('fontSize', false, fSize)

where fSize is a number from 1 to 7, corresponding to the allowable values for <font size="XXX">.

However, this does not support specifying the font size in pixels. Have seen various posts about this on the internet, but no solution has been posted, just one saying that it must be implemented using DOM+ functions.

I came up with the attached code, which works if a user has selected some text, but this depends on some text being selected.

Any ideas?
selection=document.selection.createRange();

selection.pasteHTML("<span style='font-size: 24px;'>" + selection.htmlText + "</span>");

Open in new window

0
Comment
Question by:paddycobbett
2 Comments
 
LVL 19

Expert Comment

by:erikTsomik
ID: 24795435
0
 
LVL 3

Accepted Solution

by:
Kelevra earned 500 total points
ID: 24807280
Hi,

Have you considered using JQuery in your work?

http://docs.jquery.com/CSS/css#namevalue
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now