Reputation:
So I'm a student and just very lost in my coding class. My teacher gave us this code to make it so when I click on a header, it expands into a paragraph.
He then wants us to make it so when you click on the header, the paragraph disappears again. I just cannot figure out how to do this second part, making it disappear.
This is what the 'Show Article' code looks like.
function showArticle(id) {
document.getElementById(id).style.display="block";
Can anyone help me out please? I feel like it's so simple I'm just so bad at this. If you could tell me where to place it in my code that would be much appreciated as well.
Upvotes: 1
Views: 572
Reputation: 176
If you are using js and not jquery
var visible = false;
function showArticle(id) {
if (visible === false) {
document.getElementById(id).style.display="block";
visible = true;
}
else {
document.getElementById(id).style.display="none";
visible = false;
}
}
Upvotes: 1
Reputation: 3322
Just use jQuery's toggle.
$('#'+headerId).on('click', function(){
$('#'+id).toggle();
});
Upvotes: 0
Reputation: 1140
function toggleArticle(id){
($('#'+id).is(':visible') ? $('#'+id).hide() : $('#'+id).show() )}
Upvotes: 0