because you tokenize the string based on space and then use the second tokenized string to update the header in this code:
var mysplittedtitle = clickedTitle.innerHTML.split(" "); var newTitle = "- " + mysplittedtitle[1];
adding a space changes the location of the title bar. Please note if you added a space after your "title" and did not disappear, since you did not change which element of the "title" string array is.
One way to fix this is to add a span around the "title", like this:
<div class='collapsibleCont'> <h3 id='collapsible1' class='collapsibleTitle' onclick="handleCollapsible('collapsible1')"> + <span id="title">title</span> </h3> <p style="display:none" class='collapsibleContent'>hello</p> </div>
and then change your javascript to
function handleCollapsible(id) { var clickedTitle = document.getElementById(id); var contentC = clickedTitle.parentNode.childNodes[1]; if (contentC.style.display == 'none') { contentC.style.display = 'block'; var mysplittitle = document.getElementById("title").innerHTML(); var newTitle = "- " + mysplittitle; clickedTitle.innerHTML = newTitle; } else { contentC.style.display = 'none'; var mysplittitle = document.getElementById("title").innerHTML(); var newTitle = "+ " + mysplittitle; clickedTitle.innerHTML = newTitle; } }
http://jsfiddle.net/5fdo2yzt/
Steve source share