At work we run into an interesting problem: we have a page with a dropdown list that, when the selected option changes, updates the values of a series of items from the values of a JSON variable.
The 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
. For example, if Chile is selected, the one span
with id "country" will contain "Chile", the one span
with id "phone" 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 we can use and none of them allow us to embed JavaScript (not even inline ), they only let us embed HTML and CSS.
So the question would be: how can the behavior described above be simulated, but exclusively using HTML+CSS without any JavaScript or JSON?
Regarding HTML and CSS we have no limitations: we can reorder/arrange the elements as we want, we can add new elements/remove existing elements, we can add as much code as we want... as long as it doesn't contain any script, just HTML and CSS.
For example, we finally found a solution (I'll share it in the answers below for inspiration) by replacing the select
with a list ul
, transforming the JSON to CSS (with :before
and content
) and with a series of input
hidden s... but the problem is that it takes up too much space and we want to know if there would be a different method.
Although an ingenious solution, more than a space problem, I see two drawbacks:
I suggest you turn your proposal around and leave the information inside the html body. Something like that:
This is the solution we found (I leave it here in case it helps people to see what we were looking for and/or in case it serves as a reference), although the problem with it is that it gets very complicated (the list of countries is not only 3, but 30) and ends up needing a lot of extra code.
Surely something could be done with
:target
instead of:checked
and it would simplify things (you could change alllabel
toa
and avoid all checkboxes).The idea is the following:
select
and simulate a dropdown list usingul
;input
typeradio
for each country, before the fields that we want to be updated;li
,ul
we add alabel
for each of theinput
ones we created in the previous step;:checked
and the selector~
, we add CSS rules that will add the content depending on the radius that is currently selected.The code looks like this at the end:
Using only the :target selector, it can be simplified a bit.