Hey fellas, i’m having a hard time with some functions and I’ve finally humbled myself to asking because I cant seem to find anything on google…
Here are two functions: one to show, the other to hide the menu elements.
function hide(id) {
alert("hide function");
alert(id);
var d = document.getElementById(id);
alert(d);
document.getElementById(id).style.display='none';
}
function show(id) {
var d = document.getElementById(id);
document.getElementById(id).style.display='block';
var o = document.getElementById("c" + id);
o.setAttribute( "onclick", "javascript:hide('id');" );
}
<dt onclick="javascript:show('4');" id="c4">Forth Menu Item</dt>
<dd id="4">
<ul>
<li><a href='#'>Home</a></li>
<li><a href='#'>Why partner with First Horizon Msaver?</a></li>
<li><a href='#'>Speaking Engagements</a></li>
</ul>
</dd>
This menu is loaded with the DT element onclick attribute set to show(‘4’).
When I run show(), I am wanting to add a c to the id to select the DT element and change the onclick attribute to “javascript:hide(‘4’);”
I’m really “hiding” the dd element.
I’m running into all kinds of variable problems, mainly when I get to the hide(id), it is the c+id variable (ie: hide(‘c4’) instead of hide(‘4’)
I’ve put alerts all in the show() function but can not figure out how id becomes c+id when delclaring:
o.setAttribute( "onclick", "javascript:hide('id');" );
Any input on what’s happening would be great!
(first time posting, hopefully i have code in legible format!)