HTML Basics:: For Customized Training Related To Javascript or
HTML Basics:: For Customized Training Related To Javascript or
HTML Basics:
Part 2
Slides © 2016 Marty Hall, [email protected]
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
Tables
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
Basic Template
<table border="1">
<caption>Table Caption</caption>
<tr>...</tr>
<tr>...</tr>
<tr>...</tr>
<tr>...</tr>
</table>
Simple Example
<table border="1">
<caption>Table Caption</caption>
<tr><th>Heading1</th> <th>Heading2</th></tr>
<tr><td>Row1 Col1 Data</td><td>Row1 Col2 Data</td></tr>
<tr><td>Row2 Col1 Data</td><td>Row2 Col2 Data</td></tr>
<tr><td>Row3 Col1 Data</td><td>Row3 Col2 Data</td></tr>
</table>
7
Notes on Previous Example
• Tables with borders
– Generally used for real tabular data
• Tables without borders
– Also frequently used to control alignment and layout of elements in the page
– There is trend to use divs and CSS instead, but many layout options are still easier
with tables
• Table headings
– Are not automatically blue (as in screenshot on previous slide)
• This was due to the stylesheet attached to the page
• Captions
– For the table heading, many HTML developers omit <caption> and just put normal
HTML above or below the table
9
table Element Attributes (Continued)
• width, height
– This specifies the width or height of the table, either in pixels (<table
width="250">) or, for width only, as a percentage of the current browser window
width (<table width="75%">)
• cellspacing
– The space in pixels between adjacent cells. Drawn as a 3D line if border is nonzero,
otherwise empty space in the background color is used
– The default is usually about 3
• cellpadding
– The empty space, in pixels, between the cell’s border and the table element
– The default is usually about 1
10
11
tr: Table Row
• tr is used to define each row in the table
– Each row will then contain th and/or td entries
• align
– The default horizontal alignment for table cells.
• Legal values: left, right, or center
• valign
– The default vertical alignment for table cells.
• Legal values: top, bottom, or middle
• bgcolor
– The row color. Overrides bgcolor of the table as a whole.
– Tables with rows that alternate colors are widely used, but again stylesheets are
often used instead
12
13
Table Cells: th and td – Attributes
• align
– left, right, center, justify and char.
– E.g., the following aligns entries on a decimal point
• <td align="char" char=".">
• valign
– top, bottom, middle
• width, height
– Values in pixels only (no percentages officially allowed)
• bgcolor, background
– Background color and image (tiled)
• nowrap
– Disables word wrapping. Use with caution
14
<table border="1">
<tr><th colspan="2">Col 1&2 Heading</th>
<th>Col3 Heading</th></tr>
<tr><td>Row1 Col1 Data</td>
<td rowspan="2">Row1&2 Col2 Data</td>
<td>Row1 Col3 Data</td></tr>
<tr><td>Row2 Col1 Data</td>
<td>Row2 Col3 Data</td></tr>
</table>
15
coreservlets.com – custom onsite training
Forms
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
<form action="http://localhost:8088/SomeProgram">
First name: <input name="firstName" value="J. Random"/><br/>
Last name: <input name="lastName" value="Hacker"/><br/>
<input type="submit" value="Submit Form"/>
</form>
17
Notes
• Relative URLs
– Previous example had an absolute URL for the action. Most apps use a relative URL
so that you can move the entire app to another server without editing the HTML.
• I used an absolute URL only because I was running a mini test server locally to echo
back the data it received
• Framework-specific forms
– Most Web app frameworks (JSF, .NET, PHP, Ruby on Rails etc.) have special tags
that turn into forms that submit data in the format that the framework expects
• See the JSF tutorial at coreservlets.com
• No forms at all
– Some applications use a mixture of Ajax (JavaScript that talks to the server and
updates the page) and regular form submissions (which navigate to a new page)
– Other apps use Ajax only, so few, if any, pages have forms
18
19
GET Form: Submission Result
20
21
POST Form: Initial Result
22
23
coreservlets.com – custom onsite training
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
25
Text Controls
• Textfields
– <input …/> or
– <input type="text" …/>
• value attribute gives “real” initial value
• placeholder attribute gives temporary hint that disappears when user clicks in field
• Password Fields
– <input type="password" …/>
• Always use POST
• Text Areas
– <textarea rows="…" cols="…" …>
…
</textarea>
• Interpretation of regular HTML tags turned off
between <textarea…> and </textarea>
26
Push Buttons
• Submit buttons
– <input type="submit" …/>
• Use value to change button’s label (default is something like “Submit Query”)
• Use name if you have multiple buttons in same form (see next slide)
• JavaScript buttons
– <input type="button" onclick="javaScriptFunction()" …/>
• Widely used with Ajax
• Reset buttons
– <input type="reset" …/>
• HTML-styled buttons
– <button type="submit" ...>
html
</button>
27
Multiple Submit Buttons
• Button names
– Submit buttons don’t normally need a name attribute, but if you have more than one
button and want the server to identify which one was pressed, give them names
• Applies to standard form submissions, not to buttons that invoke JavaScript
Item:
<input type="text" name="Item" value="4 TeraByte iPod"/><br/>
<input type="submit" name="Add" value="Add Item to Cart"/>
<input type="submit" name="Delete" value="Delete Item from Cart"/>
28
Check Boxes
• Format
– <input type="checkbox" …/>
• Use checked="checked" to make it initially checked
• In normal form submissions, name/value pair sent only if checkbox is checked when
form is submitted
• Example code
<br/>
<input type="checkbox" name="noEmail" checked="checked"/>
Check here if you do <i>not</i> want to
get our email newsletter
• Example result
29
Radio Buttons
• Format
– <input type="radio" name="…" value="…"…>
• All radio buttons in a group should have same name. Used even with JavaScript.
• Only one button in a group can be pressed; pressing a different one causes previous
one to pop out
• Example
<dl>
<dt>Credit Card:</dt>
<dd><input type="radio" name="creditCard" value="visa"/>
Visa</dd>
<dd>…
<dd><input type="radio" name="creditCard" value="java" checked="checked"/>
Java Smart Card</dd>
…
</dl>
30
• Example
Favorite language:
<select name="language">
<option value="c">C</option>
<option value="c++">C++</option>
<option value="java" selected="selected">Java</option>
<option value="javascript">JavaScript</option>
<option value="perl">Perl</option>
<option value="ruby">Ruby</option>
</select>
31
Multi-Select List Boxes
• Format
– Give size if you want to see
all entries at once, without scrollbars
– Specify multiple for mult-select
• Example
Languages you know:<br/>
<select name="language" multiple="multiple" size="6">
<option value="c">C</option>
<option value="c++">C++</option>
<option value="java" selected="selected">Java</option>
<option value="javascript">JavaScript</option>
<option value="perl" selected="selected">Perl</option>
<option value="ruby">Ruby</option>
32 </select>
33
coreservlets.com – custom onsite training
Miscellaneous
Elements
Slides © 2016 Marty Hall, [email protected]
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
• Syntax
<fieldset>
<legend>Title of the Etched Border</legend>
… (stuff to put inside the box) …
</fieldset>
35
Grouping Form Entries: Example
<fieldset>
<legend>ajax:updateField</legend>
<form ...>
<label for="f">Enter temperature in Fahrenheit:</label>
<input type="text" id="f"/>
<input type="button" id="convertButton" value="Convert"/>
<hr width="500" align="left"/>
<label for="c">Temperature in Celsius:</label>
<input type="text" id="c"/>
<label for="k">Temperature in Kelvin:</label>
<input type="text" id="k"/>
</form>
</fieldset>
36
• label: syntax
<label for="fname">First name:</label>
<input name="userFirstName" id="fname"/>
<label>First name:
<input name="userFirstName"
</label>
37
br
• <br/> – line breaks
– Forces a line break
• You often want to break lines in the middle of paragraphs, or force line breaks after
input elements in forms
• Multiple consecutive empty <p></p> pairs do not result in multiple blank lines. But,
multiple consecutive <br/> elements do.
• Use clear="all" to make sure that left or right aligned images or tables above do not
hand down into next paragraph
• Examples
<br/>
<br clear="all"/> [skips past hanging images]
38
hr
• <hr/> – horizontal rule
– Draws a horizontal etched/shaded line
• Examples
<hr/>
<hr width="50%"/>
<hr/>
39
coreservlets.com – custom onsite training
Wrapup
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
Summary: Tables
<table border="1">
<caption>Table Caption</caption>
<tr>...</tr>
<tr>...</tr>
<tr>...</tr>
<tr>...</tr>
</table>
41
Summary: Forms
• <form>
– Not always necessary when using JavaScript to talk to server
• Text input
– <input/> or <input type="text"/> – regular text fields
– <input type="password"/> – password fields (use POST for regular forms)
– <textarea> – multi-line text input (HTML tags ignored inside)
• Buttons
– <input type="button" onclick="…"/> – most common for JavaScript
– <input type="submit"/> – most common in regular forms
• Choosing among multiple choices
– <input type="checkbox"/>
– <input type="radio"/>
42 – <select><option>…</option>…</select>
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.