Is it always bad practice to start an identifier with a number? (CSS)

In my project, I have materials and comments, each with an identifier. Currently, identifiers are only numeric and correspond to their database identifiers. Everything works fine, but when I run it through the W3 validator, I get an error:

value of attribute "id" invalid: "1" cannot start a name

I suppose that instead I could just precede all identifiers with some string, but then whenever I used or manipulated the identifier in jQuery or PHP, I have to do id.replace ('string', '') before using It. This seems rather cumbersome. Any tips?

+8
jquery html css php
source share
5 answers

Yes, using numbers as identifiers for HTML elements is bad practice.

  • It violates the W3C specification.
    You noted this in your question, and this is true for every HTML specification except HTML5

  • This is bad for SEO.
    Search engine optimized HTML identifiers MUST reflect the content of the identified element. Check out How to compose HTML code and class names such as Rockstar by Meitar Moscovitz. This gives a good overview of this concept.

  • Server problems may occur.
    When I first started programming in ASP classic, I had to access the fields of the submit form using syntax like Request.Form("some_id") . However, if I made Request.Form(1) , it would return the value of the second field in the collection of the form, and not an element with Id equal to 1. This is a fairly standard behavior for working with collections. It is also similar to javascript and can make it difficult to run client-side scripts to support them.

+16
source share

I suggest you use the "comment-ID" or "post-ID" prefixes.

If you need an identifier in JavaScript, you just need id.substring(8) (for "comment -")

+6
source share

The HTML 5 specification removes this limitation. If you are concerned about reality, you can simply consider changing the DTD to HTML5.

http://www.w3.org/TR/html5/elements.html#the-id-attribute

+4
source share

If you are manipulating an element, you can simply use $(this).jQueryOperation() - so you can have a prefix without replacing anything!

+2
source share

The best way for your need is to have a prefix for your class, I mean something like item- x and x is the number you need.

But from my personal experience it is better to use classes for your elements, and you know that you should use classes if the element is not unique on the page

0
source share

All Articles