0

I do apologise if this question has been asked but after hours of searching I could not find a solution.

I have a snippet of JavaScript that allows a block of text to be hidden until the user clicks the read more button.

the problem is I need to change the input value attribute after click to read less than to read more after collapse.

JavaScript:

function toggleMe(a){
var e=document.getElementById(a);
if(!e)return true;
if(e.style.display=="none"){
e.style.display="block"
document.getElementsById("toggle").value = "Read Less"
}
else{
e.style.display="none"
document.getElementsById("toggle").value = "Read More"
}
return true;
}
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

<div id="Read" style="display:none">
  <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
 </div>
<br />
 <input id="toggle" type="button" onclick="return toggleMe('Read')" value="Read More">
 <br />

as you can see from snippet above I am running into a problem where I don't know properly what to change the value="" attribute.

1
  • 2
    you need to use getElementById(), not getElementsById() (remove the "s")
    – BeNdErR
    Commented Mar 17, 2015 at 11:38

2 Answers 2

5

Just a typo:

document.getElementsById()

Should be:

document.getElementById()

Docs here.

When your javascript code doens't works, its a best practice to take a look on your console before anything:

enter image description here

In your case, that is the exception thrown. If you click the right link js:25 it would point straight to the getElementsById function.

1
  • thankyou very much... must have been lack of sleep :D Commented Mar 17, 2015 at 13:51
1

function toggleMe(a){
    var e=document.getElementById(a);
    if(!e)return true;
    if(e.style.display=="none"){
        e.style.display="block"
        document.getElementById("toggle").value = "Read Less"
    } else{
        e.style.display="none"
        document.getElementById("toggle").value = "Read More"
    }
    return true;
}
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

<div id="Read" style="display:none">
  <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
</div>
<br />
<input id="toggle" type="button" onclick="return toggleMe('Read')" value="Read More">
<br />

15
  • What @DontVoteMeDown is saying (sarcastically) is that just putting fixed code without any explanation of what you've done is not really helping anybody. It's not explaining to the OP what they've done wrong, and it means people have to read/compare every single line to see what you've actually changed to fix it
    – freefaller
    Commented Mar 17, 2015 at 11:49
  • @DontVoteMeDown, I know your answer is better (did I write somewhere something else?) but do you think it's worth to write comment like this? does it give plus to anything? Commented Mar 17, 2015 at 11:49
  • 1
    Your answer adds less than my comment, actually. You could improve it, but you didn't event after my comment. Commented Mar 17, 2015 at 11:53
  • 1
    @freefaller I know, that was my bad. I removed it. Commented Mar 17, 2015 at 11:56
  • 1
    @harcos - that's a fair point, and you're right - your answer does work, even if it you don't help people with WHY it works. I am unable to remove my downvote due to the time that has passed since I did it... if you make an edit that gives at least SOME explanation, I will remove it
    – freefaller
    Commented Mar 17, 2015 at 12:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.