Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using spaces in the name attribute of an html tag

Tags:

html

My validation script I have written takes the name attribute from an input tag and puts it in an alert if that input is required and not filled out. EX:

<input type="text" name="dateRequested" class="required" />

So if this wasnt filled out by the user, they would see an alert saying 'dateRequested' is a required field. Having no space looks ugly, so, simply, I would like to know if it is possible to have a space in the name attribute....ex: name="Date Requested". And if this is possible, is it bad practice?

like image 609
Dan Avatar asked May 17 '11 16:05

Dan


People also ask

Can HTML attribute values have spaces?

From HTML5 3.2. 3.1 The id attribute: The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.

How do you put a space in HTML tag?

The simplest way to add a space in HTML (besides hitting the spacebar) is with the non-breaking space entity, written as &nbsp; or &#160;.

Can attributes have spaces?

And, of course, any data-* attribute value can contain spaces that are meaningful (including leading and trailing spaces) so those spaces have to be reflected when accessing the element using that attribute value.

Which tag is used for space in HTML?

To add real spaces to your text, you can use the &nbsp; character entity. Tip: The non-breaking hyphen (&#8209;) is used to define a hyphen character (‑) that does not break into a new line.


1 Answers

Yes it is possible. (The name attribute is defined as containing CDATA which is "pretty much whatever the heck you like"*)

No, it isn't bad practise. Well, not terrible practise. It might come back to bite you later though.

However … I would approach this by getting the id of the input, finding the <label> with a matching for attribute, and using the text content of that. Labels are designed for human friendly labels, names are not. (Using jQuery (and not testing) for laziness: $('label[for="' + $(this).attr('id') + '"]').text())

Footnote: * counter intuitively there is a NAME token which is not what the name attribute takes, but it what the id attribute uses. There is also an ID token (which the id attribute doesn't take because it takes a NAME token).

like image 197
Quentin Avatar answered Sep 28 '22 20:09

Quentin