ozvena
05-08-2004, 04:20 AM
Hi,
I need to hide a paragraph when user clicks on a different paragraph. The code below works but seems to be little bit too complicated. Is there a more elegant way? Thank you!
-----------------------------------------------------------------
function hide(elementId)
{
var ourEle = document.getElementById(elementId);
var parent = ourEle.parentNode;
var blankEleP = document.createElement("p");
var blankText = document.createTextNode("");
blankEleP.appendChild(blankText);
parent.replaceChild(blankEleP, ourEle);
}
<p onclick ="hide('hobbies');">HOBBIES</p>
<p id="hobbies"> TEST TEST TEST</p>
-----------------------------------------------------------------
I used replaceChild instead of removeChild because eventually I want to do a toggle function. May be that is not a good enough reason. Well, what do you think?
I need to hide a paragraph when user clicks on a different paragraph. The code below works but seems to be little bit too complicated. Is there a more elegant way? Thank you!
-----------------------------------------------------------------
function hide(elementId)
{
var ourEle = document.getElementById(elementId);
var parent = ourEle.parentNode;
var blankEleP = document.createElement("p");
var blankText = document.createTextNode("");
blankEleP.appendChild(blankText);
parent.replaceChild(blankEleP, ourEle);
}
<p onclick ="hide('hobbies');">HOBBIES</p>
<p id="hobbies"> TEST TEST TEST</p>
-----------------------------------------------------------------
I used replaceChild instead of removeChild because eventually I want to do a toggle function. May be that is not a good enough reason. Well, what do you think?