Skip to content
Advertisement

Dynamically link radio button tabs to content div

I initially had some tabs created with radio buttons and labels which are linked to corresponding divs using their IDs and was working fine. But the problem is I am now creating the radio button tabs and their corresponding divs dynamically and wish to still allow my css to know which one should be open when a radio button is checked.

My CSS:

JavaScript

With the above initially, when the radio button with id of tab-1 is checked, the div with id of content-1 is displayed but right now this will no longer work for me as I am now creating the radio buttons dynamically using incoming IDs from my DB like: #tab-100, #tab-101, #tab-102 and the divs as well #content-100, #content-101, #content-102. I need my CSS to be dynamic as well so when #tab-100 is checked, it automatically displays #content-100. I am aware I can do this with javascript but I want to believe that there is a way to do it with CSS.

Advertisement

Answer

I later resorted to using javascript and dynamically created a style element.

HTML:

JavaScript

JS:

JavaScript

this answer was helpful.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement