Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old September 6th, 2003, 10:58 AM
Registered User
 
Join Date: Jun 2003
Location: na, na, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Finding width of text

Hi,

I'm using JavaScript and DOM to position DIVs and SPANs on a page. It would be a lot easier if I could figure out the width of text within a DIV or SPAN without having to explicitly set it.

For example, if I have a SPAN with the text "abcde," and I haven't explicitly set the width of the SPAN, is there a way I can determine the width in pixels of either the SPAN element or the text within it? (I'm not using a fixed-width font, so I can't calculate the width based on the number of characters.)

Thank you in advance for your help!

Jeri
jerimorris2000 @ yahoo . com
  #2 (permalink)  
Old September 7th, 2003, 05:29 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Jeri,

I am not 100% sure, but I don't think you can do that. Consider the following example:
Code:
<html>
<head>
  <title>Test Width</title>
  <script language="JavaScript" type="text/javascript">
  function TestMe()
  {
    alert(document.getElementById('spTest').style.width);
  }
  </script>
</head>
<body>
  <span id="spTest">This is a test</span>
  <input type="button" value="Click" onclick="TestMe();">
</body>
</html>
If you run this, and click the button, you'll get an empty dialog box, indicating that the width is empty / null / undetermined.

If you change the span tag to this:
Code:
  <span id="spTest" style="width: 100px">
the dialog box will now show "100px". So I am afraid that it looks like the browser won't tell you how wide an element is if you haven't explicitly stated it first.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #3 (permalink)  
Old September 7th, 2003, 06:57 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

May work if you use currentStyle instead of style. Also try using offsetWidth.

--

Joe
  #4 (permalink)  
Old September 7th, 2003, 12:40 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yeah, I thought about using currentStyle too, but it returns "auto" :-(

The offsetWidth, however, does the trick:
Code:
alert(document.getElementById('spTest').offsetWidth);
returns 87 which is indeed the exact size of the text in the <span> tag. Notice that offsetWidth is used directly on the object, not on the style property as in my example.....

Nice!


Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #5 (permalink)  
Old September 8th, 2003, 02:34 PM
Registered User
 
Join Date: Jun 2003
Location: na, na, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Folks,

Thank you so much for your replies! offsetWidth is exactly what I was looking for!

Happily,

Jeri


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fixed Width text file to MSDE with C# mgudmund C# 2005 3 November 9th, 2007 05:09 AM
How to change the width of text box in a Report kishorelakkimsetty Reporting Services 3 May 13th, 2006 01:50 PM
Text Box width toshi ASP.NET 1.0 and 1.1 Basics 9 March 30th, 2006 02:20 PM
Finding Image width and height AlDugan XSLT 1 March 29th, 2006 05:37 PM
Text Box... Width MER78 BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 5 March 3rd, 2006 02:10 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.