faqts : Computers : Programming : Languages : JavaScript : Document

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

15 of 20 people (75%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

Can I have js generated HTML attributes or tags?

Feb 12th, 2000 17:39
Martin Honnen,


NN3  introduces the so called javascript entity for an HTML attribute 
value where you write
  <TAGNAME ATTRIBUTENAME="&{your js expression here};">
to have the result of the js expression as the attribute value; for 
instance
  <SCRIPT>
  var width = 200;
  </SCRIPT>
  ...
  <TD WIDTH="&{width};">
Unfortunately IE in no version supports that so to have a cross browser 
solution you need to document.write the complete tag 
  document.write('<TAGNAME ATTRIBUTENAME="' + (your js expression here) 
+ '">');
for instance
  document.write('<TD WIDTH="' + width + '">');
IE however knows so called dynamic properties where an HTML style 
attribute value is set as a js expression (comparable to the js entity) 
but the attribute is dynamically updated whenever the expression 
changes.
Example
  <SCRIPT>
  var color = new Date().getSeconds() > 30 ? 'lime' : 'red';
  setInterval("color = new Date().getSeconds() > 30 ? 'lime' : 'red'", 
              30);
  </SCRIPT>
  ...
  <P STYLE="color: expression(color);">
  Kibology for all.
  </P>
Here the style property color is set to the value of the js variable 
color which is periodically updated and these updates cause the color 
property to update too.