I'm trying to write a script to add a class attribute to an element only if another element contains a certain attribute. This is the HTML source code:
<fieldset class="step1 option0">
<legend><b>Options</b></legend>
<p>
<input id="question_1" name="group_1" type="radio" MadCap:conditions="guideConditions.ProductA" />
<label for="question_1" MadCap:conditions="guideConditions.ProductA">Option 1</label>
</p>
<p>
<input id="question_2" name="group_1" type="radio" />
<label for="question_2">Option 2</label>
</p>
</fieldset>
<fieldset id="question_1_1" class="hide step2 option1" MadCap:conditions="guideConditions.ProductA">
<legend><b>Outcome:</b>
</legend>
<p>This should be only displayed for product A</p>
</fieldset>
<fieldset id="question_1_2" class="hide step2">
<legend><b>Outcome:</b>
</legend>
<p>I want to add an "option1" class if the element with id="question_1" contains the property "MadCap:conditions". If the property is not present, "option2" should be added</p>
</fieldset>
I want the script to add an "option1" class to the fieldset with id "question_1_2" if the input element with id "question_1" contains the attribute "MadCap:conditions". Otherwise, the script should add an "option2" class (fieldset class="hide step2 option1" id="question_1_2" versus fieldset class="hide step2 option2" id="question_1_2")
This is what I've tried, but it does not seem to work:
<script type="text/javascript">
/*<![CDATA[/>
var element = document.getElementById("question_1"),
var element2 = document.getElementById("question_1_2"),
if (element.hasAttribute('MadCap:conditions')) { // "generate the class dynamically"
element2.classList.add("option1")
} else { // "increment it by one"
element2.classList.add("option2")
}
/]]>*/
</script>
Any ideas on how to fix this? Thank you!