I'm migrating a website that use Javacsript/HTML/PHP using reusable javascript code, in certain moment I saw the opportunity to simplify code in functions that use almost the same code.
Let's say: I want to display values from the database into various <span>
or <div>
elements using AJAX. all the referred elements use the same layout to display the data, the difference only resides in the element id or a minimal procedure to validate data. That's all.
However, I quickly came to the conclusion that I don't have a way to refer a specific element in HTML without use the id
property, and the only alternative that I think was the use of name
property. I know that this property it's only for <input>
in a <form>
,and because this elements are not <input>
this rule doesn't apply (I know that in HTML the rules are not enforced).
The basic idea is to stop using id
s to make my code more reusable.
To pass from this:
<div id='sGroupName1'>
+---<div id='elementA1'>
| +---<span id='txtA1'>
+---<div id='elementB1'>
+---<span id='txtB1'>
<div id='sGroupName2'>
+---<div id='elementA2'>
| +---<span id='txtA2'>
+---<div id='elementB2'>
+---<span id='txtB2'>
to this:
<div id='sGroupName1'>
+---<div name='elementA'>
| +---<span name='txtA'>
+---<div name='elementB'>
+---<span name='txtB'>
<div id='sGroupName2'>
+---<div name='elementA'>
| +---<span name='txtA'>
+---<div name='elementB'>
+---<span name='txtB'>
Doing my code in this way, i think will break the HTML5 conformity rules. I know this isn't a problem per se, because I have solved in not orthodox way, but I want to hear what is the best way to deal with this situations.
I know that exist a question related HTML - Alternative for ID when ID is only unique within a certain scope? but in this case I don't want to deal with classes and CSS rules because the current page is using a lot of CSS and there are a lot Javascript function in the page that manipulates the CSS class, using Class as kind of identifier, I think would lead to some bugs (but not so sure).