Pr.Pg Next Pg

HTML Interview Questions and Answers Part 2

11.How can I use tables to structure forms?

  • Small forms are sometimes placed within a TD element within a table.

  • This can be a useful for positioning a form relative to other content, but it doesn't help position the form-related elements relative to each other.

  • To position form-related elements relative to each other, the entire table must be within the form. You cannot start a form in one TH or TD element and end in another.

  • You cannot place the form within the table without placing it inside a TH or TD element.

  • You can put the table inside the form, and then use the table to position the INPUT, TEXTAREA, SELECT, and other form-related elements, as shown in the following example.


<FORM ACTION="[URL]">
<TABLE BORDER="0">
<TR>
<TH>Account:</TH>
<TD><INPUT TYPE="text" NAME="account"></TD>
</TR>
<TR>
<TH>Password:</TH>
<TD><INPUT TYPE="password" NAME="password"></TD>
</TR>
<TR>
<TD> </TD>
<TD><INPUT TYPE="submit" NAME="Log On"></TD>
</TR>
</TABLE>
</FORM>

 

12.How do I center a table?

  • In your HTML, use

<div class="center">
<table>...</table>
</div>

 

  • In your CSS, use

div.center {

text-align: center;

}

div.center table {

margin-left: auto;

margin-right: auto;

text-align: left;

}

13.How do I use forms?

  • The basic syntax for a form is: <FORM ACTION="[URL]">...</FORM>
    When the form is submitted, the form data is sent to the URL specified in the ACTION attribute.

  • This URL should refer to a server-side (e.g., CGI) program that will process the form data.

  • The form itself should contain
    at least one submit button (i.e., an <INPUT TYPE="submit" ...> element),
    form data elements (e.g., <INPUT>, <TEXTAREA>, and <SELECT>) as needed, and
    additional markup (e.g., identifying data elements, presenting instructions) as needed.

 

14.How can I check for errors?

  • HTML validators check HTML documents against a formal definition of HTML syntax and then output a list of errors.

  • Validation is important to give the best chance of correctness on unknown browsers (both existing browsers that you haven't seen and future browsers that haven't been written yet).
    HTML checkers (linters) are also useful.

  • These programs check documents for specific problems, including some caused by invalid markup and others caused by common browser bugs. Checkers may pass some invalid documents, and they may fail some valid ones.
    All validators are functionally equivalent; while their reporting styles may vary, they will find the same errors given identical input.

  • Different checkers are programmed to look for different problems, so their reports will vary significantly from each other.

  • Also, some programs that are called validators (e.g. the "CSE HTML Validator") are really linters/checkers.

  • They are still useful, but they should not be confused with real HTML validators.
    When checking a site for errors for the first time, it is often useful to identify common problems that occur repeatedly in your markup.

  • Fix these problems everywhere they occur (with an automated process if possible), and then go back to identify and fix the remaining problems.

  • Link checkers follow all the links on a site and report which ones are no longer functioning. CSS checkers report problems with CSS style sheets.

 

15.Do I have to memorize a bunch of tags?

  • No. Most programs that help you write HTML code already know most tags, and create them when you press a button. But you should understand what a tag is, and how it works.

  • That way you can correct errors in your page more easily.

 

16.How do I make a form so it can be submitted by hitting ENTER?

  • The short answer is that the form should just have one <INPUT TYPE=TEXT> and no TEXTAREA, though it can have other form elements like checkboxes and radio buttons.

 

17.How do I set the focus to the first form field?

  • You cannot do this with HTML. However, you can include a script after the form that sets the focus to the appropriate field, like this:

<form id="myform" name="myform" action=...>
<input type="text" id="myinput" name="myinput" ...>
</form>

<script type="text/javascript">
document.myform.myinput.focus();
</script>

  • A similar approach uses <body onload=...> to set the focus, but some browsers seem to process the ONLOAD event before the entire document (i.e., the part with the form) has been loaded.

 

18.How can I use tables to structure forms?

  • Small forms are sometimes placed within a TD element within a table.

  • This can be a useful for positioning a form relative to other content, but it doesn't help position the form-related elements relative to each other.

  • To position form-related elements relative to each other, the entire table must be within the form. You cannot start a form in one TH or TD element and end in another.

  • You cannot place the form within the table without placing it inside a TH or TD element.

  • You can put the table inside the form, and then use the table to position the INPUT, TEXTAREA, SELECT, and other form-related elements, as shown in the following example.


<form action="[URL]">
<table border="0">
<tr>
<th scope="row">
<label for="account">Account:</label>
</th>
<td>
<input type="text" name="account" id="account">
</td>
</tr>
<tr>
<th scope="row">
<label for="password">Password:
</th>
<td>
<input type="password" name="password" id="password">
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Log On"></td>
</tr>
</table>
</form>

 

19.Can I have two or more actions in the same form?

  • No. A form must have exactly one action. However, the server-side (e.g., CGI) program that processes your form submissions can perform any number of tasks (e.g., updating a database, sending email, logging a transaction) in response to a single form submission.

 

20.How can I use forms for pull-down navigation menus?

  • There is no way to do this in HTML only; something else must process the form. JavaScript processing will work only for readers with JavaScript-enabled browsers.

  • CGI and other server-side processing is reliable for human readers, but search engines have problems following any form-based navigation.

Pr.Pg Next Pg