Passing VBScript Variables to HTML Attributes
There may be scenarios where the value stored in a VBScript variable has to be passed as a value to an HTML tag attribute. For example, if a user wanted to set the color of webpage using a form, the user input could be accepted through a textbox, stored in a VBScript variable, and then would have to be assigned to the appropriate HTML attribute for the process to work.
However, as HTML and VBScript are not compatible with each in terms of code, the developer needs to find another way to pass the contents of the variable to the attribute in question.
Webpages are always rendered in HTML, even when there is dynamic scripting in the code. The code is converted into HTML as well, and then the webpage is displayed. Therefore, to pass the contents of a variable to an HTML attribute, the developer needs to first extract the contents, and then treat it as text when assigning it.
An illustrative example:
tablecellspace = InputBox("Cellspadding for Table: ")
document.write("<table cellspadding =" & cellspadding & " border = 1>")
The Dim statement declares the variable ‘tablecellspace’ for use. The next statement gets a value from the user, through the use of an input box, and stores in the variable.
The document.write statement indicates that there is output to the webpage. The contents between the parentheses indicate the content that is to be printed on the webpage. All string values are enclosed in quotes, however, even though the variable is treated like text, it is inserted into the function without quotes and hence the contents of the variable will be displayed. If the variable was put into quotes, the name of the variable would become a string.