Today I was pseudo-coding a we and then I tried to make a hide/show content function for mobile browser.
The problem is (not my hardcoded thing), it's the delay of the button to hide or show content and I think it's because it's not previously initialized.
HTML:
<article id="horarios">
<h2>Horarios <a onclick="showContent(parentNode.parentNode.id, this.id)" class="showContentBtn" id="showContentBtnHr"></a></h2>
<span id="hide-horarios">
About my hardcode thing I just make that "parent, parent id" (Horarios) concat with "hide-", I don't know a better solution now but in some way it's ok.
Now here's the problem:
JS:
function showContent(value, value2){
var value="hide-"+value;
var content = document.getElementById(value).style.display;
if(content == "none"){
document.getElementById(value).style.display = "block";
document.getElementById(value2).innerHTML = "";
}
else{
document.getElementById(value).style.display = "none";
document.getElementById(value2).innerHTML = "";
}
}
CSS
#hide-programas, #hide-horarios, #hide-aboutus{
display: none;
}
When I test it, I have to click one time and it do nothing then it works. I think this is because "content" not have any value on it to compare so there's nothing to do the first time and then it have a value.
It's possible to fix it? I know it will hurt your eyes but this weird sh*t works in some way.
.style.displayreturns an empty string the first time, as it's not set.parentNode.parentNode.idunless you've initialised a global parentNode variable, perhaps you meanthis.parentNode.parentNode.id. And why would you do that just to get the ID so you can call getElementById, why not just pass the elements?showContent(this.parentNode.parentNode, this)then just use el0 and el1 in the function, removing all those redundant document.getElementById calls.