Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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
Reply With Quote
  #2 (permalink)  
Old September 7th, 2003, 05:29 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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.
Reply With Quote
  #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
Reply With Quote
  #4 (permalink)  
Old September 7th, 2003, 12:40 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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.
Reply With Quote
  #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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:47 AM.


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