So I have been wrestling quite some time with this and starting to stare myself blind here.
Maybe this is trivial and I bet I am missing something basic so here goes.
I have a simple html document with two paragraphs, both are class=show with the id's: id=show1 & id:show2, in the .css file I have .show { display:none; }.
So on to my problem, When I expand the text I want my link to switch text to 'Hide information' (Dölj information to be exact) and when I collapse it, then it should revert back to 'Show more information' (Visa mera information to be exact)
When I had the links inside the html document this was ok but when I moved both links inside the Javascript file and created them dynamicly it did not work anymore and I get a error with null statement.
Here is my Javascript:
function newLink() {
//Create the link
newLink = document.createElement('a');
//Give the link a Id
newLink.id = 'show1_link';
//Set the href
newLink.href = 'javascript:showHide(this.id,elemId)';
//Create a variable for the link text
var linkText = document.createTextNode('Visa mera information');
//Append the text to the link
newLink.appendChild(linkText);
//Create a variable for the paragraph
var elem = document.getElementById('show1')
//Insert the text before the paragraph with the Id show2
elem.parentNode.insertBefore(newLink,show1);
}
addLoadEvent(newLink);
function showHide(link_id,elemId) {
var link = document.getElementById(link_id);
var text = document.getElementById(elemId);
//Make the switch from none -> block & back
text.style.display = (text.style.display == 'block') ? 'none' : 'block';
//Check the status of the text
var status = (text.style.display == 'block') ? 'none' : 'block';
text.style.display = status;
//Change the text accordingly
link.innerHTML = (status == 'block') ? 'Dölj information' : 'Visa mera infomration';
}
addLoadEvent(showHide);
The link and function works if I remove the variables and display status but then it does not change the link text. The text.style.display returns null, what I don't get is that this script worked with the links hard coded into the html document.
Any help appreciated.