Javascript variable as a width/height

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Javascript variable as a width/height

Post by Parody »

Firstly, I have no idea what this would be called so I stood no chance of finding this out on google so I'm sorry if this is obvious or has been asked before.

I'm pretty new to javascript and I don't really fancy learning it in its entirety, but small snippets help me greatly. I understand variables and writing them and can implement ajax, but one thing that I really would like to do is evading me. I would like to use a javascript variable as a height or width of a cell/row/table. I know you can set the style width of a cell or row with javascript, but is there any way whatsoever to use:

Code: Select all

<table><tr><td width=myvar>Cell</td></tr></table>
???

(Obviously with myvar already defined)

I understand if you tell me to forget about it and just define it in the cell's style using javascript, but this is so much easier on a large scale if I could do this. Do I just need to use some special syntax or is it just not possible?

Thanks
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Javascript variable as a width/height

Post by Ambush Commander »

You can either output the HTML code using JavaScript writeln, or modify the attribute by retrieving the element using DOM and then editing its attributes. Google around and you'll see some examples. A framework like jQuery will simplify things greatly.
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Re: Javascript variable as a width/height

Post by Parody »

Thanks, but I realised that I needed the variables to constantly calculated due to changes elsewhere so it had to be done by defining the style in a function. I'm sure this will come in handy soon though.
Post Reply