0

HTML has 13 form elements and the input element has 23 different types. My question is what is the reasoning behind making a form element a type of input or its own element?

At first I thought maybe its because all input types are just variants on input type=text, but that doesn't make sense when you throw checkbox and radio into the mix. Perhaps they would be better as their own form elements?

<input type="checkbox" name="check" value="yes">
// Would become
<checkbox name="check" value="yes">

Another form element that doesn't make sense to me is textarea for two reasons.

  1. According to How to change the Content of a with Javascript you can use element.value to both set and retrieve a textarea's contents. Yet textarea doesn't have the value attribute, at least its not shown in the DOM.

  2. The way textareas behave when resetting a form is inconsistent with other form elements. All input elements will be reset to the value of their value attribute. But a textarea is reset to whatever its 'value' (quotes because it doesn't have a value attribute in the DOM) was set as when the page loaded.

Example 1 (consistent with input): If you click the reset button, both input and 'textarea' will be reset to their 'initial' values (Example use jQuery). JSFiddle

<form>  
    <input type="text" name="text" value="initial" />
   <textarea>initial</textarea>
   <button type="reset">Reset</button>
</form>

<script>
    $('input, textarea').val('set by javascript');
</script>

Example 2 (inconsistent with input): You cannot use .attr('value') with textarea elements, which means the following example doesn't work. You have to use $('textarea').text() or $('textarea').html() in order to set the default value.

<form>  
    <input type="text" name="text" value="initial" />
   <textarea>initial</textarea>
   <button type="reset">Reset</button>
</form>

<script>
    $('input, textarea').attr('value', 'set by javascript');
</script>

There are other things that don't make sense to me but I will only highlight these two in my question. I'm hoping someone can explain to me why HTML and Javascript treat form elements differently (specifically when its comes to getting, setting, and resetting their values), and why certain form elements have their own elements and why others are just types of the input element.

Community
  • 1
  • 1
Craig Harshbarger
  • 1,863
  • 3
  • 18
  • 29
  • 2
    Just a heads up, if you can't make this about solving a specific problem, it'll probably be closed as off-topic/non-constructive. – Shawn Erquhart Jun 23 '15 at 17:30
  • Craig..its probably just 'legacy built upon' that got us to where we are with what seem like redundant yet, differently treated objects in HTML. This happens quite often with programming languages although in HTML the deprecation of objects/tags is usually driven by the Javascript that can handle them. Browsers pretty much all honor old tags/objects in HTML. – MiiinimalLogic Jun 23 '15 at 17:32
  • @ShawnErquhart - is my question not at least educational? This is something I've struggled to understand when coding my own projects. – Craig Harshbarger Jun 23 '15 at 17:34
  • @MiiinimalLogic - Yes, I understand that. But input and textarea have been around from the beginning haven't they? I would expect them to be more consistent. – Craig Harshbarger Jun 23 '15 at 17:35
  • True, but textarea and input are completely different albeit subtleties like Rows and Columns. Check out :http://www.martinrinehart.com/frontend-engineering/engineers/html/html-tag-history.html – MiiinimalLogic Jun 23 '15 at 17:40
  • Thanks for that resource. – Craig Harshbarger Jun 23 '15 at 17:51
  • 1
    @CraigHarshbarger it's definitely educational, it's just off topic. Some of the most valuable resources on SO are off topic, actually. Here's a great example: http://stackoverflow.com/questions/11246/best-resources-to-learn-javascript – Shawn Erquhart Jun 23 '15 at 18:36

0 Answers0