7

Earlier, I was creating a form and mindlessly named one of my inputs "name", similar to the following pseudo code:

<input id="name" type="text" name="name" />

I then remembered that in the past, I've had issues with inputs with the name "action." If I remember correctly, the issues arose during enhancement with JavaScript (there was confusion between the form's action and the input with the name "action").

So my questions are:

  • Is it safe to use something similar to the snippet above?
  • Is there some canonical list of strings that should generally NOT be used as input names?
Jim D
  • 988
  • 10
  • 18
  • if you are using jquery to target them it will probably be ok with any valid name – Huangism Apr 30 '13 at 20:12
  • 2
    I would say avoid common attributes of the form and also the method names that are attached to the form. Common problem is [Submit Is Not A Function](http://stackoverflow.com/a/834197/14104) – epascarello Apr 30 '13 at 20:17

1 Answers1

3

There's mostly problems if you're using shortcuts like myform.action as action is a property of the form element (like are id, parentNode, and some other properties you can see using console.dir(myform)).

You should always use the fast and reliable way :

var inputElement = document.getElementById('action');

or, using jQuery :

var $input = $('#action');

As noticed by epascarello, you'll also have problem if you call or use a property that you overrided by defining an element with that name, for example submit. But as you're the one calling it in your code, it should be obvious if it happens.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 2
    getElementById is not going to save you 100% of the time. Name an element submit and call the submission via JavaScript. The name will override the method. – epascarello Apr 30 '13 at 20:14