Page 1 of 1

Javascript variable as a width/height

Posted: Sat Mar 08, 2008 4:10 pm
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

Re: Javascript variable as a width/height

Posted: Sat Mar 08, 2008 5:35 pm
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.

Re: Javascript variable as a width/height

Posted: Sun Mar 09, 2008 3:50 am
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.