1. Create a new file in Dreamweaver called lab6_js_properties.html
2. Add script tags between the body tags of the web page.
3. Declare a text string to the variable txt.
By using the .big() and .small() properties on the txt variable, you can change the size of the text string. This txt string is then printed to the screen using document.write statements.
Review the image above and experiment with the different ways to manipulate the text string with JavaScript.
One of the common uses for this is take a user's input, and convert to lower case and then check it against a certain value.
Save the file and upload to your server.
1. Create a new file in Dreamweaver called lab6_js_menu.html.
2. Type the following code between the body tags to create a html form and an empty results div:
<form>
Select your favorite music:
<select >
<option>Jazz</option>
<option>Classical</option>
<option>Rock</option>
<option>Hip Hop</option>
</select>
</form>
<div id="results"></div>
This creates an HTML form with a single form element, a select menu. In this tutorial, you are going to create an script that will print a text string within the results div based on the item selected in the menu.
1. First step is to add script tags between your head tags so you can create a custom function. I called this function displayResult() and gave it the selTag parameter. The selTag parameter will contain the "this" keyword which will be used to select the current menu.
<script type="text/javascript">
function displayResult(selTag)
{
var x=selTag.options[selTag.selectedIndex].text;
}
</script>
2. Between the curly brackets, a variable x was declared. When the "this" keyword is passed through to the selTag parameter, the statement will be interpreted by the browser as follows:
this.options[this.seletedIndex].text;
This statement is saying in the current options form element return the text of the selected item.
The next step is to take variable x and display it within a text string within the results div. To achieve this first you need to select the results div using document.getElementById method. Then use the innerHTML property to add the text string to the div.
An onChange event handler is used to call the function and pass the this keyword.
<select onchange="displayResult(this)">
Save the file and upload to your server.
The template file contains an embedded stylesheet that defines the background color of the page and the width, border, and background color of the form with the id guestbook.
The template also contains an html form with text boxes, radio buttons, a text area box and two buttons. Review the html code within the template on how to create an html form.
Look at the code view of the guestbook template file. Notice that the form has the unique Id, guestbook while each form element has it's own unique id. For example, id=name or id=comments.
Add the thankyou() function and review the CSS code
<script>
function thankyou() {
}
</script>
In the first part of the thankyou function, several variables are created that store the contents of the users input from the form. There are two common ways to achieve this in JavaScript. The most popular way is to use the getElementbyId
1) In the first example, the value of the text book names first, in the form named guestbook with the html document displayed in the browser window is stored in the variable fname.
2) In the second example, the form element with the Id of name is stored in the lname variable.
A new variable is created called OpenWindow. This variable contains the window.open() statement. The open() method contains three parameters separated by comas. First is the name of the file or URL of the new window.In this example, it is blank because the new window will display dynamic content not a static page. The second parameter is the name of the window, "newwin" and finally the third parameter applies the attributes of the window.
var OpenWindow = window.open("","newwin","height=400,width=600");
Several document.write statements are used to build the html necessary to display the dynamic code into the OpenWindow browser window.
OpenWindow.document.write("<html><head>");
OpenWindow.document.write("<title>Thanks for Writing</title>");
OpenWindow.document.write("</head>");
OpenWindow.document.write("<body bgcolor='#900'>");
OpenWindow.document.write("<div id='guestbook''>");
OpenWindow.document.write("<p>Thank you <b>" +fname+ " " +lname+ "</b> from " +usermail+ " for filling out my guestbook.</p>");
OpenWindow.document.write("<p>Your message: <br>" +usercomments);
OpenWindow.document.write("</p><form><input type='button' value='Close Window' onClick='self.close()'></form>");
OpenWindow.document("</div></body></html>");
Add the onClick event handler to the send button to call the thankyou() function.
Save the file as lab6_guestbook.html