On a wordpress website I’d like to display "button one" when the url doesn’t contain "/en/" path, and display "button two" when url contains "/en/".
The button’ html look like this:
<a href="https://www.google.com" class="microwidget-btn mini-button header-elements-button-1 show-on-desktop" >Button one</span></a>
<a href="https://www.yahoo.com" class="microwidget-btn mini-button header-elements-button-2 show-on-desktop" >Button two</span></a>
I’ve been trying many variations of this approach:
<script type="text/javascript">
if(window.location.href.indexOf("/en/") > -1) {
document.getElementsByClassName("microwidget-btn mini-button header-elements-button-1 show-on-desktop").style.display = 'none';
document.getElementsByClassName("microwidget-btn mini-button header-elements-button-2 show-on-desktop").style.display = '';
} else {
document.getElementsByClassName("microwidget-btn mini-button header-elements-button-1 show-on-desktop").style.display = '';
document.getElementsByClassName("microwidget-btn mini-button header-elements-button-2 show-on-desktop").style.display = 'none';
}
</script>
But nothing happen and in the console I get
Uncaught TypeError: document.getElementsByClassName(…)[0] is undefined
Thank you
3
Answers
You could use JavaScript to check if the url contains a specific language like so
Please notice that I changed your html code. I added an id property and style tag with display: none as default
you can use this wordpress code for it
You can use WordPress code for it: