I've not done javascript in 8 years so bear with me.
I'm doing an FAQ page where I toggle the visibility of answers when the user clicks on the questions.
I currently use:
in conjunction with the following HTML:
What I would like to do is modify togvis() so that I don't need to generate/pass an explicit id. I'd like the call to be "togvis(this)" and have togvis() pick the following element in the DOM (actually the last sibling) and toggle that element's visibility.
With that in mind, I tried:
and
This doesn't work.
When I throw in some debug alert() calls:
e.nodeType is 1
e.nodeName is DIV
e.nodeValue is null
The first two I expect. The third should be the content "FAQ Answer1", no ?
What am I doing wrong?
How do I achieve what I was looking for?
I'm doing an FAQ page where I toggle the visibility of answers when the user clicks on the questions.
I currently use:
Code:
function togvis(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
return false;
}
in conjunction with the following HTML:
Code:
<ol>
<li>
<a href="#" onclick="return togvis('faq1');">FAQ Question 1?</a>
<div class="faq" id="faq1" >FAQ Answer1</div>
</li>
</ol>
What I would like to do is modify togvis() so that I don't need to generate/pass an explicit id. I'd like the call to be "togvis(this)" and have togvis() pick the following element in the DOM (actually the last sibling) and toggle that element's visibility.
With that in mind, I tried:
Code:
function togvis2( e1 ) {
var e = e1.parentNode.lastChild;
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
return false;
}
Code:
<li>
<a href="#" onclick="return togvis2(this);">FAQ Question 1?</a>
<div class="faq">FAQ Answer1</div>
</li>
This doesn't work.
When I throw in some debug alert() calls:
e.nodeType is 1
e.nodeName is DIV
e.nodeValue is null
The first two I expect. The third should be the content "FAQ Answer1", no ?
What am I doing wrong?
How do I achieve what I was looking for?