I'm looking to easily switch visibility between two different divs on my webpage. The default display should be div id2, but when a user clicks a link, I want div id1 to replace it. I've tried various methods and even tinkered with it on jsfiddle, but for some reason I can't seem to get it right.
Here's the HTML code snippet:
<a href="#id" onclick="toggle_visibility(id1, id2);">Change Payment Method</a>
<div id="id1" style="display: none"><p>test 1</p></div>
<div id="id2"><p>test 2</p></div>
And here's the JavaScript function:
function toggle_visibility(id1, id2) {
var e = document.getElementById(id1);
var e2 = document.getElementById(id2);
if(e.style.display == 'block') {
e.style.display = 'block';
e2.style.display = 'none';
}
else {
e.style.display = 'none';
e2.style.display = 'block';
}
}