How can I group labels in a semantic way? e.g.:
If I was making a form about favorite foods, as in:
<form>
<label for="food1">Rice</label>
<input type="checkbox" id="food1" name="food"><br>
<label for="food2">Pasta</label>
<input type="checkbox" id="food2" name="food"><br>
<label for="food3">Meat</label>
<input type="checkbox" id="food3" name="food"><br><br>
<input type="submit" name="submit" value="submit">
</form>
Would I put a <h2>Favorite foods</h2> before them or is there any more semantically appropriate tag for it?
The question is pretty general and kind of boils down to preference, but you could use a fieldset tag to group the fields, and then use a legend tag for the header.
https://www.w3schools.com/tags/tag_fieldset.asp
https://www.w3schools.com/tags/tag_legend.asp
<form>
<fieldset>
<legend>Favorite Foods</legend>
<label for="food1">Rice</label>
<input type="checkbox" id="food1" name="food"><br>
<label for="food2">Pasta</label>
<input type="checkbox" id="food2" name="food"><br>
<label for="food3">Meat</label>
<input type="checkbox" id="food3" name="food"><br><br>
</fieldset>
<input type="submit" name="submit" value="submit">
</form>
Related
Is it possible to have multiple radio button groups in a single form? Usually selecting one button deselects the previous, I just need to have one of a group deselected.
<form>
<fieldset id="group1">
<input type="radio" value="">
<input type="radio" value="">
</fieldset>
<fieldset id="group2">
<input type="radio" value="">
<input type="radio" value="">
<input type="radio" value="">
</fieldset>
</form>
Set equal name attributes to create a group;
<form>
<fieldset id="group1">
<input type="radio" name="group1">value1</input>
<input type="radio" name="group1">value2</input>
</fieldset>
<fieldset id="group2">
<input type="radio" name="group2">value1</input>
<input type="radio" name="group2">value2</input>
<input type="radio" name="group2">value3</input>
</fieldset>
</form>
This is very simple you need to keep different names of every radio input group.
<input type="radio" name="price">Thousand<br>
<input type="radio" name="price">Lakh<br>
<input type="radio" name="price">Crore
</br><hr>
<input type="radio" name="gender">Male<br>
<input type="radio" name="gender">Female<br>
<input type="radio" name="gender">Other
Just do one thing,
We need to set the name property for the same types. for eg.
Try below:
<form>
<div id="group1">
<input type="radio" value="val1" name="group1">
<input type="radio" value="val2" name="group1">
</div>
</form>
And also we can do it in angular1,angular 2 or in jquery also.
<div *ngFor="let option of question.options; index as j">
<input type="radio" name="option{{j}}" value="option{{j}}" (click)="checkAnswer(j+1)">{{option}}
</div>
in input field make name same
like
<input type="radio" name="option" value="option1">
<input type="radio" name="option" value="option2" >
<input type="radio" name="option" value="option3" >
<input type="radio" name="option" value="option3" >
To create a group of inputs you can create a custom html element
window.customElements.define('radio-group', RadioGroup);
https://gist.github.com/robdodson/85deb2f821f9beb2ed1ce049f6a6ed47
to keep selected option in each group, you need to add name attribute to inputs in group, if you not add it then all is one group.
Is it possible to have multiple radio button groups in a single form? Usually selecting one button deselects the previous, I just need to have one of a group deselected.
<form>
<fieldset id="group1">
<input type="radio" value="">
<input type="radio" value="">
</fieldset>
<fieldset id="group2">
<input type="radio" value="">
<input type="radio" value="">
<input type="radio" value="">
</fieldset>
</form>
Set equal name attributes to create a group;
<form>
<fieldset id="group1">
<input type="radio" name="group1">value1</input>
<input type="radio" name="group1">value2</input>
</fieldset>
<fieldset id="group2">
<input type="radio" name="group2">value1</input>
<input type="radio" name="group2">value2</input>
<input type="radio" name="group2">value3</input>
</fieldset>
</form>
This is very simple you need to keep different names of every radio input group.
<input type="radio" name="price">Thousand<br>
<input type="radio" name="price">Lakh<br>
<input type="radio" name="price">Crore
</br><hr>
<input type="radio" name="gender">Male<br>
<input type="radio" name="gender">Female<br>
<input type="radio" name="gender">Other
Just do one thing,
We need to set the name property for the same types. for eg.
Try below:
<form>
<div id="group1">
<input type="radio" value="val1" name="group1">
<input type="radio" value="val2" name="group1">
</div>
</form>
And also we can do it in angular1,angular 2 or in jquery also.
<div *ngFor="let option of question.options; index as j">
<input type="radio" name="option{{j}}" value="option{{j}}" (click)="checkAnswer(j+1)">{{option}}
</div>
in input field make name same
like
<input type="radio" name="option" value="option1">
<input type="radio" name="option" value="option2" >
<input type="radio" name="option" value="option3" >
<input type="radio" name="option" value="option3" >
To create a group of inputs you can create a custom html element
window.customElements.define('radio-group', RadioGroup);
https://gist.github.com/robdodson/85deb2f821f9beb2ed1ce049f6a6ed47
to keep selected option in each group, you need to add name attribute to inputs in group, if you not add it then all is one group.
Edit: Because of the time restriction of the project I just had to revert to a working copy of the code and redo all my work, but I would still like to know what the reason is for this for the future.
So, I've had this problem now for a little while, of one of the dags in this HTML file I've been working on where a from and div tags would get underlined by my editor (Komodo) saying something to the effect of "Unexpected End Tag (ignored). This wasn't causing much problems so I just ignored it...until the editor started highlighting the closing body tag with the same thing and the file doesn't actually work correctly (using JQM) anymore.
The thing I don't understand is that I haven't been touching the HTML AT ALL. In the past when this happened (as it has before), I just pulled a previous version from the repo I'm using and that would resolve the issue, but this time I haven't made a commit in a while and I really want to find a solution to this problem rather than a quick fix.
Here is a screenshot of the syntax error I'm getting via my editor:
Here is the HTML for the form, if it matters:
<div data-role="content">
<img src="img/logo.png" alt="RPG Tracker">
<form action="" method="post" id="addCharForm">
<fieldset data-role="controlgroup">
<label for="dateCreated">Date Created:</label>
<input type="date" name="dateCreated" id="dateCreated">
</fieldset>
<fieldset data-role="controlgroup">
<label for="charAge">Age:</label>
<input type="text" name="charAge" id="charAge" value="" class="required number">
</fieldset>
<fieldset data-role="controlgroup">
<label for="charName">Name:</label>
<input type="text" name="charName" id="charName" value="" class="required">
</fieldset>
<fieldset data-role="controlgroup" data-type="horizontal">
<legend>Choose character gender:</legend>
<input type="radio" name="gender" id="radioMale" value="Male">
<label for="radioMale">Male</label>
<input type="radio" name="gender" id="radioFemale" value="Female">
<label for="radioFemale">Female</label>
</fieldset>
<fieldset data-role="controlgroup">
<label for="charAttrs">Describe your character's <b>attributes</b> in this field:</label>
<textarea id="charAttrs" name="charAtts"></textarea>
</fieldset>
<fieldset data-role="controlgroup">
<label for="charSkills">Describe your character's <b>skills</b> in this field:</label>
<textarea id="charSkills" name="charSkills"></textarea>
</fieldset>
<fieldset data-role="controlgroup">
<label for="charBio">Character Biography:</label>
<textarea id="charBio" name="charBio"></textarea>
</fieldset>
<fieldset data-role="controlgroup">
<label for="charRating">Rate Your Character:</label>
<input type="range" name="charRating" id="charRating" value="100" min="0" max="100">
</fieldset>
<input type="reset" value="Reset">
<input type="submit" value="Submit Character" data-theme="b">
</form>
</div>
As requested, here's the full html:
http://pastebin.com/tsE0MuP7
The input and img tags are self closing, so I suggest closing them all and seeing if that fixes it. (I suspect it won't, but it's worth a try).
e.g.
<img src="img/logo.png" alt="RPG Tracker" />
and
<input type="date" name="dateCreated" id="dateCreated" />
The issue could be elsewhere in the file too, so a copy of the entire HTML would be preferable. It will probably be an open tag somewhere (e.g. using <div> instead of </div>).
<fieldset data-role="controlgroup">
<label for="charName">Name:</label>
<input type="text" name="charName" id="charName" value="" class="required">
</fieldset>
<fieldset data-role="controlgroup" data-type="horizontal">
<legend>Choose character gender:</legend>
<input type="radio" name="gender" id="radioMale" value="Male">
<label for="radioMale">Male</label>
<input type="radio" name="gender" id="radioFemale" value="Female">
<label for="radioFemale">Female</label>
</fieldset>
<fieldset data-role="controlgroup">
<label for="charAttrs">Describe your character's <b>attributes</b> in this field:</label>
<textarea id="charAttrs" name="charAtts"></textarea>
</fieldset>
<fieldset data-role="controlgroup">
<label for="charSkills">Describe your character's <b>skills</b> in this field:</label>
<textarea id="charSkills" name="charSkills"></textarea>
</fieldset>
<fieldset data-role="controlgroup">
<label for="charBio">Character Biography:</label>
<textarea id="charBio" name="charBio"></textarea>
</fieldset>
<fieldset data-role="controlgroup">
<label for="charRating">Rate Your Character:</label>
<input type="range" name="charRating" id="charRating" value="100" min="0" max="100">
</fieldset>
<input type="reset" value="Reset">
<input type="submit" value="Submit Character" data-theme="b">
</form>
</div>
I would like to implement something similar to a google search with radio buttons. Depending on the radio button selected, would change the type of search (search,images,video, etc).
Right now I have:
<div id ="searchtext">
<form method="get" id ="searchbox" action="http://www.google.com/search"/>
<input type="text" name="q" size="30" class="searchtext" maxlength="255" value="" />
<input type="image" value="Search" class="searchbutton" src="images/searchbar/searchbutton.png"/>
<br/>
</div>
<div id="radiosearch">
<input type="radio" name="radiosearch" onclick="document.searchbox.action='http://www.google.com/search?q=';" checked="checked"/> Web
<input type="radio" name="radiosearch" onclick="document.searchbox.action='http://images.google.com/images?q=';"/>Images
<input type="radio" name="radiosearch" onclick="document.searchbox.action='http://maps.google.com/maps?hl=en&tab=wl?q=';"/>Maps
<input type="radio" name="radiosearch" onclick="document.searchbox.action='http://www.youtube.com/results?q=';"/>Youtube
<span class = "class1">
Change Theme
</span>
</div>
However, clicking on the radio boxes is not changing the form action. Any Ideas?
Try
<input type="radio" name="radiosearch" onclick="document.getElementById('searchbox').action='http://www.google.com/search?q=';" checked="checked"/> Web
<input type="radio" name="radiosearch" onclick="document.getElementById('searchbox').action='http://images.google.com/images?q=';"/>Images
<input type="radio" name="radiosearch" onclick="document.getElementById('searchbox').action='http://maps.google.com/maps?hl=en&tab=wl?q=';"/>Maps
<input type="radio" name="radiosearch" onclick="document.getElementById('searchbox').action='http://www.youtube.com/results?q=';"/>Youtube
This is one thing that I really hate within development is forms. Below is my code and what I am trying to do is align the inputs with the labels Name: input. Is there a rule that you use to help remember when coding forms?
CSS:
#newwebsiteForm form{
padding:10px;
margin:10px 0;
width:480px;
position: relative;
}
#newwebsiteForm label{
width:240px;
display:block;
float:right;
}
#newwebsiteForm input{
width:240px;
display:block;
float:left;
}
HTML:
<section id="content">
<h1>Free Quote</h1>
<p>Please fill out the below questionnaire to receive your free web development quote</p>
<form action="" method="post" accept-charset="utf-8">
<select name="requiredOption" id="requiredOption">
<option id="pleaseselect" value="pleaseselect">Please Select Your Required Quote</option>
<option id="newwebsite" value="newwebsite">New Website</option>
<option id="websiteredevelopment" value="websiteredevelopment">Website Redevelopment</option>
<option id="other" value="other">Other</option>
</select>
</form>
<div id="newwebsiteSection">
<form action="" id="newwebsiteForm" method="get" accept-charset="utf-8">
<fieldset>
<label>Do You Require Hosting?</label><input type="radio" name="Yes" value="Yes">Yes</input><input type="radio" name="No" value="No">No</input>
<label>Do You Require A Domain?</label><input type="radio" name="Yes" value="Yes">Yes</input><input type="radio" name="No" value="No">No</input>
<label>Do You Have A Logo?</label><input type="radio" name="Yes" value="Yes">Yes</input><input type="radio" name="No" value="No">No</input>
<label>What is your Domain?</label>
<input type="url" name="domain" value="http://example.com"></input></div>
<label>Type of site Required?<label>
<select name="newwebsiteType" id="newwebsiteType">
<option value="shoppingCart">Shopping Cart</option>
<option value="CMS">Content Management System</option>
<option value="static">Static Website</option>
<option value="otherDevelopment">Other Development</option>
</select>
<label>Do You Require A Design?</label>
<input type="radio" name="Yes" value="Yes">Yes</input><input type="radio" name="No" value="No">No</input>
<label>Three Favorite colors?</label>
<input name="color1" value=""></input>
<input name="color2" value=""></input>
<input name="color3" value=""></input>
<label>What are your favorite websites?</label>
<input type="text" name="fav1" value=""></input>
<input type="text" name="fav2" value=""></input>
<input type="text" name="fav3" value=""></input>
<label>Comments?</label>
<textarea name="comments" id="comments"></textarea>
<input type="submit" name="submit" value="Send Quote Request">
</form>
</div>
<div id="websiteredevelopmentSection">
<p>Website Redevelopment</p>
</div>
<div id="otherSection">
<p>Other</p>
</div>
</section>
Just because tables are the easier option doesn't make using them right.
Here's a great article about css form design.
http://www.alistapart.com/articles/prettyaccessibleforms
The author suggests storing each label/input element in an ordered list element keeping them grouped.
I've just used this to implement a bunch of forms for various mini sites and it worked a treat!
You can align the label next to the input or above just by changing the ol li label element's display property from display:inline-block to display:block respectively.
Getting the text to align next to a radio button or checkbox can be a bit tricky but is possible by adding and styling a span element.
BEST OF ALL IT'S CROSS BROWSER COMPATIBLE!
Hope that helps.
This is probably related to the question: <div> usage correctly? Can't get the columns to line up: You can also check my comments there for some reference when dealing with semantically-correct forms.
The approach you will need to be in habit of is always structure your markup correctly first before jumping to any CSS (or styling).
A <form> is composed of the following:
The <form> itself.
The <fieldset>: acts a the container of the different sections of your <form>
The <legend>: acts as the heading of the <fieldset>
The <input />: for fields, checkboxes, radio buttons, and submit button
The <button>: an alternative for <input type="submit">, which can wrap something
inside of it.
The <select>: a list of values inside a drop-down menu.
The <label>: from the name itself, the label of the <input />, <button> and <select>
To illustrate, you can check this example:
<form>
<fieldset>
<legend>Form Heading: </legend>
<fieldset>
<legend>Group 1 Heading: </legend>
<label for="input-id">Input Label: </label>
<input id="input-id" />
</fieldset>
<fieldset>
<legend>Group 2 Heading: </legend>
<label for="select-id">Select Label: </label>
<select id="select-id">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
</fieldset>
<input type="submit" value="Submit" />
</fieldset>
</form>
With the exception of radio (<input type="radio" />) and checkboxes (<input type="checkbox" />), where the <label> should come after the <input />
You have to use the label element as follows to correctly align them:
<FORM action="http://somesite.com/prog/adduser" method="post">
<P>
<LABEL for="firstname">First name: </LABEL>
<INPUT type="text" id="firstname"><BR>
<LABEL for="lastname">Last name: </LABEL>
<INPUT type="text" id="lastname"><BR>
<LABEL for="email">email: </LABEL>
<INPUT type="text" id="email"><BR>
<INPUT type="radio" name="sex" value="Male"> Male<BR>
<INPUT type="radio" name="sex" value="Female"> Female<BR>
<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>
I wrap each input/label pair in a <div>. This helps a lot with styling.
<div class="form-item">
<label for="first_name">First Name</label>
<input type="text" name="first_name" id="first_name" />
</div>