setExpression

HTML (DHTML)

setExpression Method

Internet Development Index

Sets an expression for the specified object.

Syntax

object.setExpression(sPropertyName, sExpression [, sLanguage])

Parameters

sPropertyName Required. String that specifies the name of the property to which sExpression is added.
sExpression Required. String that specifies any valid script(JScript, JavaScript, VBSCript) statement without quotations or semicolons. This string can include references to other properties on the current page. Array references are not allowed on object properties included in this script.
sLanguage Optional. String that specifies one of the following values:
JScriptDefault. Language is JScript.
VBScriptLanguage is VBScript.
JavaScriptLanguage is JavaScript.

Return Value

No return value.

Remarks

Use the setExpression method to add expressions to supported Cascading Style Sheets (CSS) attributes and read/write DHTML Properties. To remove expressions set by setExpression, use the removeExpression method.

The following syntax sections show how to set an expression on DHTML Properties and CSS attributes.

  • Use this syntax to set an expression on a read/write property or on an expando property.
    object.setExpression(sPropertyName, sExpression, sLanguage)
  • Use this syntax to set an expression on a CSS attribute.
    object.style.setExpression(sPropertyName, sExpression, sLanguage)
  • Use the expression() syntax to set an expression on a CSS attribute in HTML.
    <ELEMENT STYLE="sAttributeName:expression(sExpression)">

The data type of the evaluated expression in the sLanguage parameter must match one of the possible values allowed for the sExpression parameter. If the property or attribute specified by the first parameter requires a string, the data type of the second parameter must be a string. Otherwise, the second parameter is evaluated prior to invoking setExpression, causing the expression to be set to the result of the evaluation.

Use the uniqueID property of an object in an expression to refer back to the object. Using uniqueID is an alternative to specifying an id for expressions that use an object reference.

The cssText property is a unique property that is not compatible with the dynamic properties implementation. Do not use cssText with any dynamic property methods.

The following examples illustrate common problems encountered with the sExpression parameter in setExpression. The sExpression appears valid, but may not be.

  • The provided sExpression is invalid because document.style.fontSize is "npx" and will not add to 13
    object.style.setExpression("height","document.style.fontSize + 13"); 
  • The provided sExpression is invalid when document.body.style.fontSize is previously unspecified
    object.style.setExpression("width","document.body.style.fontSize"); 

Examples

The following examples use the setExpression method to change the width of a blue box. In each example, the width of the blue box is equal to the sum of the values of the first two text boxes. When a value in one of the text boxes changes, the width of the blue box recalculates.

This example shows the HTML implementation of setExpression.

<INPUT TYPE=text ID=oBox1 value=40>The sum of the values in
these two text boxes determines the width<BR>
<INPUT TYPE=text ID=oBox2 value=40>of the blue text box below.
<BR><INPUT TYPE=text ID=oBox3
STYLE="width:expression(eval(oBox1.value) +
eval(oBox2.value));background-color:blue">
<BR><INPUT TYPE=button ID=Button
value="Click to resize blue box above"
onclick="recalc()">
This feature requires Microsoft® Internet Explorer 5 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

This example uses the uniqueID property and the setExpression method to update the innerText property of a table row.

<SCRIPT>
window.onload=fnInit;
function fnInit(){
var iLen=oSheet.rows.length-1;
for(var i=1;i<iLen;i++){
var oRow=oSheet.rows[i];
var oCells=oRow.cells;
oCells(3).setExpression("innerText",
"fnGetValue(" + oRow.uniqueID + ")");
}
var oGrand=oSheet.rows(iLen).cells(1);
oGrand.setExpression("innerText","fnGetTotal()");
}
function fnGetTotal(){
var iValue=0;
var iLen=oSheet.rows.length-1;
for(var i=1;i<iLen;i++){
iValue+=parseFloat(oSheet.rows(i).cells(3).innerText);
}
return iValue;
}
function fnGetValue(oRow){
var oCells=oRow.cells;
var oPrice=oCells(2);
var oQuantity=oCells(1);
var sPrice;
var sQuantity;
if(oPrice.childNodes[0].nodeName=="#text"){
sPrice=oPrice.innerText;
}
else{
var vPrice=oPrice.childNodes[0].value;
sPrice=(vPrice==""?"0":vPrice);
}
if(oQuantity.childNodes[0].nodeName=="#text"){
sQuantity=oQuantity.innerText;
}
else{
var vQuantity=oQuantity.childNodes[0].value;
sQuantity=(vQuantity==""?"0":vQuantity);
}
var vAlg1=parseFloat(sPrice) * parseFloat(sQuantity);
return vAlg1;
}
</SCRIPT>
<TABLE ID="oSheet">
<TR><TH>Product</TH><TH>Quantity</TH>
<TH>Price per #</TH><TH>Totals</TH></TR>
<TR><TD>Browser Putty</TD><TD EDIT="true">2</TD>
<TD EDIT="true">4.99</TD><TD></TD></TR>
<TR><TH COLSPAN=3>Grand Total</TH><TH></TH></TR>
</TABLE>
This feature requires Microsoft® Internet Explorer 5 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

Standards Information

There is no public standard that applies to this method.

Applies To

A, ACRONYM, ADDRESS, APPLET, AREA, B, BDO, BIG, BLOCKQUOTE, BODY, BR, BUTTON, CAPTION, CENTER, CITE, CODE, COL, COLGROUP, currentStyle, CUSTOM, DD, DEL, DFN, DIR, DIV, DL, DT, EM, EMBED, FIELDSET, FONT, FORM, hn, HR, I, IFRAME, IMG, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=hidden, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, INS, KBD, LABEL, LEGEND, LI, LISTING, MARQUEE, MENU, NOBR, OBJECT, OL, OPTION, P, PRE, Q, RT, RUBY, runtimeStyle, S, SAMP, SELECT, SMALL, SPAN, STRIKE, STRONG, style, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TR, TT, U, UL, VAR

See Also

About Dynamic Properties, getExpression, removeExpression, recalc