I have an option select that is supposed to change the div content by matching the name of the ID in my HTML (in monthchosen
) with the value that has been chosen by the user.
The only problem I'm having is that I do not know how to check the option value against the div id using plain javascript. The final part months[i].id.includes(monthchosen)
is incorrect, though hopefully gets across what I am trying to accomplish. I have content under div ids named after the option select (ie: <div id="oct2016">)
that I would like to show when the names correspond.
My example uses React, though a solution in pure JS might suffice.
My JavaScript:
updatedate:function(){
var months = document.getElementsByClassName('month');
var monthchosen = this.refs.datechosen;
for (var i = 0; i < months.length; i++){
months[i].style.display = 'none';
months[i].id.includes(monthchosen).style.display = 'block';
}
}
My HTML
<select ref="datechosen" id="datechosen" onChange={this.updatedate}>
<option value="nov2016">November 2016</option>
<option value="oct2016">October 2016</option>
</select>