I use Divi Theme in WordPress.
I have sections that I gave IDs.
I have a select, and for each option value I use the sections IDs.
I want show one section by changing the select option and hide the other section that is showed.
Here is the select :
<select name="years" id="mySelect" onchange="myFunction()">
<option value="">TOUTES LES ANNÉES </option>
<option value="section2020">2020</option>
<option value="section2019">2019</option>
<option value="section2018">2018</option>
<option value="section2017">2017</option>
<option value="section2016">2016</option>
<option value="section2015">2015</option>
</select>
Here is the javascript :
<script>
function myFunction() {
var x = document.getElementById("mySelect").value;
if (x.style === "display:none;") {
x.style = "display:block";
} else {
x.style = "display:none;";
}
}
</script>
Could you tell my why it’s not working ?
thanks
Caroline
2
Answers
In your code, you are trying to style a string value
x.style
If we see closely
var x = document.getElementById("mySelect").value;
this is returning a string value not an html element. But yes we can use this string value to get html element and make it visible and hide other.So thank you so much @Dupinder Singh. After playing with the code, I got it to work on my DIVI theme.
However, since you can’t put in-line CSS styles on a Section in DIVI, I did the following. I went under each section and under the Advanced Tab -> Custom CSS -> Main Content = display: none;
Then since each section appeared like so I changed the javascript to the following:
The following was my HTML for the Selection Menu:
You can see it in action here: first1thousand.com/resources