passing width in <div> style to javascript

J

John

Here's some Javascript

var ff=document.getElementById('one').style.width;
alert(ff);

and this is my <div>

<div id='one' style='width:200px'>testing</div>

It does not work. Is there anyway I can pass the width value to the
javascript?

Regards
John
 
J

Jukka K. Korpela

I suspect you are running your code before the element exists.

Sounds probable.

Such problem can easily be recognized by looking at the console error
log - and if someone doesn't know how to find it in his favorite
browser, it's time to learn! If that takes too long, there's always the
possibility of wrapping the JavaScript code into...

try {
// all the code here
} catch(e) {
alert(e);
}

Browsers are mostly silent about JavaScript errors because there are so
many of them. But in program development, there is hardly any reason not
to look at error messages!
 
D

dhtml

11.9.2011 12:39, Andrew Poulos wrote:

 > I suspect you are running your code before the element exists.

Sounds probable.

Such problem can easily be recognized by looking at the console error
log - and if someone doesn't know how to find it in his favorite
browser, it's time to learn!

There was once an FAQ hosted on jibbering.com/faq that listed how to
enable error reporting for various browsers. SInce we don't have an
FAQ now, we're all (including the OP) SOL. Sorry, buddy. Try and
figure out how to turn on error reporting in your browser. Get a
debugger, too.


If that takes too long, there's always the
possibility of wrapping the JavaScript code into...

try {
   // all the code here

} catch(e) {
   alert(e);
}
You can do that, but won't get anything meaningful out of IE versions;
just "[object Error]". e.name, e.message, but really, what you want is
all the information, including the line number. Turn on error
reporting in your browser.
Browsers are mostly silent about JavaScript errors because there are so
many of them.

So many errors, you mean? That's a reason users would want to hide
them, sure.

But in program development, there is hardly any reason not
to look at error messages!
Right. Turn on error reporting in your browser. Find the error
console. Get a debugger.
 
J

John

Andrew Poulos said:
Try

window.onload = function() {
var ff = document.getElementById('one').style.width;
window.alert(ff);
};

as I suspect you are running your code before the element exists.

Andrew Poulos

Hi

Yes - you're righht. I had placed the line before the function I was
calling.

Many thanks,

Regards
John
 
D

Dr J R Stockton

In comp.lang.javascript message <94611152-7ef1-4c32-bf64-3e36a7a1908a@f2
4g2000prb.googlegroups.com>, Sun, 11 Sep 2011 10:51:23, dhtml
There was once an FAQ hosted on jibbering.com/faq that listed how to
enable error reporting for various browsers. SInce we don't have an
FAQ now, we're all (including the OP) SOL. Sorry, buddy. Try and
figure out how to turn on error reporting in your browser. Get a
debugger, too.

There IS a FAQ hosted at jibbering (seen 2011-09-12 17:29 GMT approx).
There is a copy on my site at $clj-faq.htm.
There is a small amount in my js-debug.hrm.
 
M

Matt McDonald

There was once an FAQ hosted on jibbering.com/faq that listed how to
enable error reporting for various browsers. SInce we don't have an
FAQ now, we're all (including the OP) SOL.

In response to the server(s) that host the FAQ sporadically acting up,
I've taken to hosting my own copy of the C.L.J. FAQ. It's about 90-95%
complete, only missing example pages and a few orphaned notes sections.

I will continue to pull pages from the old FAQ when I find the time (and
if I'm able to access it).

Here's the FAQ on my host:
http://www.fortybelow.ca/hosted/comp-lang-javascript/faq/
 
D

Dr J R Stockton

Fri said:
In response to the server(s) that host the FAQ sporadically acting up,
I've taken to hosting my own copy of the C.L.J. FAQ. It's about 90-95%
complete, only missing example pages and a few orphaned notes sections.

I will continue to pull pages from the old FAQ when I find the time
(and if I'm able to access it).

Here's the FAQ on my host: http://www.fortybelow.ca/hosted/comp-lang-
javascript/faq/


That alleges itself to be a real copy of Garrett's FAQ as at 2010-10-08.
If, as you say, it is not a complete and accurate copy, you should
remove it immediately.

It seems a complete waste of effort, since, as I have repeatedly written
here, I have a complete copy (without the horrible CSS) of Garrett's
last version at my $clj-faq.htm.

I also have some alternate entries, AISB, at js-faq-u.htm, and a version
annotated with some of the suggestions made here at js-faq-a.htm.

I've not recently found <http://jibbering.com/faq/> to be unavailable,
I've just accessed it.
 
D

Dr J R Stockton

In comp.lang.javascript message <kbSdndwHH_Ok6OvTnZ2dnUVZ_jWdnZ2d@westne
t.com.au>, Mon, 19 Sep 2011 08:48:20, Andrew Poulos
I just tried and couldn't get to it.

It's working for me now. Have any other Australians tried it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top