Know the āsearchā Element
Published on JunĀ 19, 2024, filed under development, html (feed). (Share this on Mastodon orĀ Bluesky?)
You know that at the moment, there are 112 elements in HTML (not counting math
, svg
, and custom elements, which is important to note). Do you know element #112, i.e., the latest member of the HTML elements family?
What element? The
search
elementāas of this writing, the latest element addition to HTML, which became part of HTML on March 24, 2023.What does it do? What does it mean?
Hereās its definition:
The
search
element represents a part of a document or application that contains a set of form controls or other content related to performing a search or filtering operation. This could be a search of the website or application; a way of searching or filtering search results on the current web page; or a global or Internet-wide search function.However, note:
Itās not appropriate to use the
search
element just for presenting search results, though suggestions and links as part of āquick searchā results can be included as part of a search feature. Rather, a returned web page of search results would instead be expected to be presented as part of the main content of that web page.Now, can we find the element in use somewhere?
From searching GitHub,
search
doesnāt seem popular yet. However, there are implementations of it. Hereās one:<search> <form action="./search/"> <label for="movie">Find a Movie</label> <input type="search" id="movie" name="q" /> <button type="submit">Search</button> </form> </search>
Is this valid?
Yes. Likewise, at the time of this chapterās writing (March 2024), the W3C validator complains (āElement
search
not allowed as child of elementbody
in this contextā), but that looks like an issue with the validator (cf. validator issue #1569). That is, this code isnāt per se bad.However, itās not the maximumāor minimum, I supposeāwe could get out of this, either. That would look more like the following:
<search> <form action=./search/> <label for=movie>Find a movie</label> <input type=search id=movie name=q> <button>Search</button> </form> </search>
What changed?
Dropped quotes: If this is the first book youāre reading from this series, all superfluous code (except for whitespace) is being removed. That includes quotes that arenāt strictly necessary. A basic rule I like to work with is that when an attribute value doesnāt contain equal signs or spaces, itās usually okay to skip the quotes. (Itās not precise, but works well enough.)
Removed value defaults: Thereās no need to set attributes with values that match the respective attributeās default. In the case of the
button
element, this istype=submit
(more accurately, the āSubmit Buttonā state.Tweaked case: Assuming the label is not a heading, using title case seems unusual. Iāve adjusted to sentence case.
This isnāt necessarily all we could do. For example, the
label
could surround theinput
, allowing us to save the labelāinput association (id
andfor
). But thatās something we donāt know, and while I do like to make some guesses and assumptions, Iād stop here. Hisearch
element!
This is from a short book, Upgrade Your HTML V, which I released just six weeks ago.
If you like this, check out the book; if you more-than-like this, check out the series (Amazon, Apple Books, Kobo, Google Play Books, Leanpub)! Cheers!
About Me
Iām Jens (long: Jens Oliver Meiert), and Iām a web developer, manager, and author. Iāve been working as a technical lead and engineering manager for companies youāve never heard of and companies you use every day, Iām an occasional contributor to web standards (like HTML, CSS, WCAG), and I write and review books for OāReilly and Frontend Dogma.
I love trying things, not only in web development and engineering management, but also in other areas like philosophy. Here on meiert.com I share some of my experiences and views. (I value you being critical, interpreting charitably, and giving feedback.)