The setAttribute Method


The setAttribute Method

The 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

Method

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

setAttribute( attribute , value [, case ])

                   
       

x

   

x

x

x

x

 

No return value

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.

graphics/06fig07.gif



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net