Solved

Javascript: How can i apply font size in pixels?

Posted on 2009-07-07
2
465 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

635 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