At work we find an interesting problem: we have a page with a drop-down list that, when the selected option changes, updates the values of a series of elements from the values of a variable in JSON.
HTML is something like this (simplified):
<select id="paises">
<option value="chile">Chile</option>
<option value="colombia">Colombia</option>
<option value="ecuador">Ecuador</option>
</select>
<div id="valores">
Pais: <span id="pais"></span><br/>
Teléfono: <span id="telefono"></span><br/>
Email: <span id="email"></span>
</div>
And the JSON is something like this (simplified):
info = {
"chile": {
"nombre": "Chile",
"telefono": "123456789",
"email": "[email protected]"
},
"colombia": {
"nombre": "Colombia",
"telefono": "192837465",
"email": "[email protected]"
},
"ecuador": {
"nombre": "Ecuador",
"telefono": "987654321",
"email": "[email protected]"
}
}
The JavaScript code is not relevant to the question, but the idea is simple: depending on the selected country, the span
will be updated. For example, if you select Chile, the span
with id "country" will contain "Chile", the span
with id "telefono" will show "123456789", etc, etc.
Now that page has been moved to a new CMS and, for different reasons, we are limited in the components that we can use and none of them allows us to incorporate JavaScript (not even inline ), it only leaves us Enter HTML and CSS.
Then the question would be: How can you simulate the behavior described above, but exclusively using HTML + CSS without any JavaScript or JSON?
Regarding HTML and CSS we have no limitations: we can reorder / organize the elements as we want, we can add new elements / delete existing elements, we can add as much code as we want ... as long as it does not contain any script, only HTML and CSS.
For example, we finally find a solution (I'll share it in the answers below in case someone inspires it) substituting the select
for a list ul
, transforming the JSON to CSS (with :before
and content
) and with a series of input
s hidden ... but the problem is that it takes up too much space and we want to know if there would be another different method.