Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display different buttons depending on url match

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&quot; class="microwidget-btn mini-button header-elements-button-1 show-on-desktop" >Button one</span></a>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<a href="https://www.yahoo.com&quot; 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

>Solution :

You could use JavaScript to check if the url contains a specific language like so

document.addEventListener("DOMContentLoaded", function() {
  if(window.location.href.indexOf("/en/") > -1) {
    // en page
    // Show button 2
    document.getElementById('button-one').style.display = 'none';
    document.getElementById('button-two').style.display = 'flex';
  } else {
    //Show button 1
    document.getElementById('button-one').style.display = 'flex';
    document.getElementById('button-two').style.display = 'none';
  }
});

Please notice that I changed you html code. I added an id property and style tag with display: none as default

<a href="https://www.google.com" id="button-one" class="microwidget-btn mini-button header-elements-button-1 show-on- desktop" style="display:none" >Button one</span></a>

<a href="https://www.yahoo.com" id="button-two" class="microwidget-btn mini-button header-elements-button-2 show-on-desktop" style="display:none">Button two</span></a>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading