The setAttribute MethodThe setAttribute method sets the value of a specified attribute. You invoke this method on an element, passing it the name of an attribute, a new value for the attribute, and an optional case argument, which, if true, makes the browser search for the attribute in a case-sensitive way. You can see the support for this method in Table 6.48. Table 6.48. The setAttribute Method
Here's an example that sets the VALUE attribute of a text field, which inserts text into that text field: (Listing 06-11.html on the web site)<HTML> <HEAD> <TITLE>Using the setAttribute Method</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- function setAttr() { document.form1.text1.setAttribute("VALUE", "Hello there!") } // --> </SCRIPT> </HEAD> <BODY> <H1>Using the setAttribute Method</H1> <FORM NAME="form1"> <INPUT TYPE="TEXT" ID="text1" SIZE="50"></INPUT> <BR> <INPUT TYPE="BUTTON" ONCLICK="setAttr()" VALUE="Set Attribute"></INPUT> </FORM> </BODY> </HTML> You can see the results in Figure 6.7, where we've inserted Hello there! into a text field. Figure 6.7. Using the setAttribute Method.
|