Onclick display of class elements not working












1















I'm trying to get descriptions of acronyms (tag===p) to display when I press the button (tag===button):






function myFunction() {
for (let i = 0; i > x.length; i++) {

var x = ;
x.push(document.getElementsByClassName("toggleAcronym"));
for (let j = 0; j > x.length; j++)
if (x[i][j].style.display === "none") {
x[i][j].style.display = "block";
}
}
}

#p6Acronyms {
width: 50%;
border: 1px solid blue;
margin: 0 auto;
padding: 20px;
}

.toggleAcronym {
display: none;
}

<!DOCTYPE HTML5>
<html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Intellectual Principles</title>
<meta name="description" content="sats">
<meta name="author" content="satser">
<link rel="stylesheet" type="text/css" href="Principles.css">
<script type="text/javascript" src="Principles.js"></script>
</head>

<body>

<div id="p6Acronyms">
<button onclick="myFunction()">RWE</button>
<p class="toggleAcronym">Real World Example</p>
<button onclick="myFunction()">CRUD</button>
<p class="toggleAcronym">Create, Read, Update, Delete</p>
<button onclick="myFunction()">CNS</button>
<p class="toggleAcronym">Central Nervous System</p>
<button onclick="myFunction()">MPS</button>
<p class="toggleAcronym">Muscle Protein Synthesis</p>
<button onclick="myFunction()">I.e.</button>
<p class="toggleAcronym">In essence</p>
<button onclick="myFunction()">ALAP</button>
<p class="toggleAcronym">As Long As Possible</p>
<button onclick="myFunction()">AMAP</button>
<p class="toggleAcronym">As Much As Possible</p>
<button onclick="myFunction()">CoC</button>
<p class="toggleAcronym">Contents of Consciousness</p>
<button onclick="myFunction()">RR(P)(F)-R</button>
<p class="toggleAcronym">Risk Reward (Probability)(Fragility) - Ratio</p>
<button onclick="myFunction()">AoL</button>
<p class="toggleAcronym">Area of Life (=Intellectual, Physical, Relationships & Intellectual)</p>
<button onclick="myFunction()">MBS</button>
<p class="toggleAcronym">Mind Body & Spirit</p>
<button onclick="myFunction()">QoC</button>
<p class="toggleAcronym">Quality of Consciousness</p>
<button onclick="myFunction()">PFC</button>
<p class="toggleAcronym">Pre-Frontal Cortex</p>
<button onclick="myFunction()">SRV</button>
<p class="toggleAcronym">Survival & Reproduction Value. //(Often used to describe factors directional effect on this.)</p>
<button onclick="myFunction()">P/T-R</button>
<p class="toggleAcronym">Practice/Theory-Ratio</p>
</div>

</body>

</html>





I suspect the JS is incorrect but can't figure out how to get it right. I don't know how to push every element of a certain class to an array and then for every element in that array, if button is clicked, display paragraph. I'm new to JavaScript and don't understand what to do with this error message:




"message": "Uncaught TypeError: Cannot read property 'length' of undefined"




, thanks for the help.










share|improve this question

























  • You need to define variable x in this line for (let i=0; i>x.length; i++) { but you have an x variable already in this line var x=

    – McBern
    Nov 24 '18 at 11:36
















1















I'm trying to get descriptions of acronyms (tag===p) to display when I press the button (tag===button):






function myFunction() {
for (let i = 0; i > x.length; i++) {

var x = ;
x.push(document.getElementsByClassName("toggleAcronym"));
for (let j = 0; j > x.length; j++)
if (x[i][j].style.display === "none") {
x[i][j].style.display = "block";
}
}
}

#p6Acronyms {
width: 50%;
border: 1px solid blue;
margin: 0 auto;
padding: 20px;
}

.toggleAcronym {
display: none;
}

<!DOCTYPE HTML5>
<html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Intellectual Principles</title>
<meta name="description" content="sats">
<meta name="author" content="satser">
<link rel="stylesheet" type="text/css" href="Principles.css">
<script type="text/javascript" src="Principles.js"></script>
</head>

<body>

<div id="p6Acronyms">
<button onclick="myFunction()">RWE</button>
<p class="toggleAcronym">Real World Example</p>
<button onclick="myFunction()">CRUD</button>
<p class="toggleAcronym">Create, Read, Update, Delete</p>
<button onclick="myFunction()">CNS</button>
<p class="toggleAcronym">Central Nervous System</p>
<button onclick="myFunction()">MPS</button>
<p class="toggleAcronym">Muscle Protein Synthesis</p>
<button onclick="myFunction()">I.e.</button>
<p class="toggleAcronym">In essence</p>
<button onclick="myFunction()">ALAP</button>
<p class="toggleAcronym">As Long As Possible</p>
<button onclick="myFunction()">AMAP</button>
<p class="toggleAcronym">As Much As Possible</p>
<button onclick="myFunction()">CoC</button>
<p class="toggleAcronym">Contents of Consciousness</p>
<button onclick="myFunction()">RR(P)(F)-R</button>
<p class="toggleAcronym">Risk Reward (Probability)(Fragility) - Ratio</p>
<button onclick="myFunction()">AoL</button>
<p class="toggleAcronym">Area of Life (=Intellectual, Physical, Relationships & Intellectual)</p>
<button onclick="myFunction()">MBS</button>
<p class="toggleAcronym">Mind Body & Spirit</p>
<button onclick="myFunction()">QoC</button>
<p class="toggleAcronym">Quality of Consciousness</p>
<button onclick="myFunction()">PFC</button>
<p class="toggleAcronym">Pre-Frontal Cortex</p>
<button onclick="myFunction()">SRV</button>
<p class="toggleAcronym">Survival & Reproduction Value. //(Often used to describe factors directional effect on this.)</p>
<button onclick="myFunction()">P/T-R</button>
<p class="toggleAcronym">Practice/Theory-Ratio</p>
</div>

</body>

</html>





I suspect the JS is incorrect but can't figure out how to get it right. I don't know how to push every element of a certain class to an array and then for every element in that array, if button is clicked, display paragraph. I'm new to JavaScript and don't understand what to do with this error message:




"message": "Uncaught TypeError: Cannot read property 'length' of undefined"




, thanks for the help.










share|improve this question

























  • You need to define variable x in this line for (let i=0; i>x.length; i++) { but you have an x variable already in this line var x=

    – McBern
    Nov 24 '18 at 11:36














1












1








1








I'm trying to get descriptions of acronyms (tag===p) to display when I press the button (tag===button):






function myFunction() {
for (let i = 0; i > x.length; i++) {

var x = ;
x.push(document.getElementsByClassName("toggleAcronym"));
for (let j = 0; j > x.length; j++)
if (x[i][j].style.display === "none") {
x[i][j].style.display = "block";
}
}
}

#p6Acronyms {
width: 50%;
border: 1px solid blue;
margin: 0 auto;
padding: 20px;
}

.toggleAcronym {
display: none;
}

<!DOCTYPE HTML5>
<html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Intellectual Principles</title>
<meta name="description" content="sats">
<meta name="author" content="satser">
<link rel="stylesheet" type="text/css" href="Principles.css">
<script type="text/javascript" src="Principles.js"></script>
</head>

<body>

<div id="p6Acronyms">
<button onclick="myFunction()">RWE</button>
<p class="toggleAcronym">Real World Example</p>
<button onclick="myFunction()">CRUD</button>
<p class="toggleAcronym">Create, Read, Update, Delete</p>
<button onclick="myFunction()">CNS</button>
<p class="toggleAcronym">Central Nervous System</p>
<button onclick="myFunction()">MPS</button>
<p class="toggleAcronym">Muscle Protein Synthesis</p>
<button onclick="myFunction()">I.e.</button>
<p class="toggleAcronym">In essence</p>
<button onclick="myFunction()">ALAP</button>
<p class="toggleAcronym">As Long As Possible</p>
<button onclick="myFunction()">AMAP</button>
<p class="toggleAcronym">As Much As Possible</p>
<button onclick="myFunction()">CoC</button>
<p class="toggleAcronym">Contents of Consciousness</p>
<button onclick="myFunction()">RR(P)(F)-R</button>
<p class="toggleAcronym">Risk Reward (Probability)(Fragility) - Ratio</p>
<button onclick="myFunction()">AoL</button>
<p class="toggleAcronym">Area of Life (=Intellectual, Physical, Relationships & Intellectual)</p>
<button onclick="myFunction()">MBS</button>
<p class="toggleAcronym">Mind Body & Spirit</p>
<button onclick="myFunction()">QoC</button>
<p class="toggleAcronym">Quality of Consciousness</p>
<button onclick="myFunction()">PFC</button>
<p class="toggleAcronym">Pre-Frontal Cortex</p>
<button onclick="myFunction()">SRV</button>
<p class="toggleAcronym">Survival & Reproduction Value. //(Often used to describe factors directional effect on this.)</p>
<button onclick="myFunction()">P/T-R</button>
<p class="toggleAcronym">Practice/Theory-Ratio</p>
</div>

</body>

</html>





I suspect the JS is incorrect but can't figure out how to get it right. I don't know how to push every element of a certain class to an array and then for every element in that array, if button is clicked, display paragraph. I'm new to JavaScript and don't understand what to do with this error message:




"message": "Uncaught TypeError: Cannot read property 'length' of undefined"




, thanks for the help.










share|improve this question
















I'm trying to get descriptions of acronyms (tag===p) to display when I press the button (tag===button):






function myFunction() {
for (let i = 0; i > x.length; i++) {

var x = ;
x.push(document.getElementsByClassName("toggleAcronym"));
for (let j = 0; j > x.length; j++)
if (x[i][j].style.display === "none") {
x[i][j].style.display = "block";
}
}
}

#p6Acronyms {
width: 50%;
border: 1px solid blue;
margin: 0 auto;
padding: 20px;
}

.toggleAcronym {
display: none;
}

<!DOCTYPE HTML5>
<html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Intellectual Principles</title>
<meta name="description" content="sats">
<meta name="author" content="satser">
<link rel="stylesheet" type="text/css" href="Principles.css">
<script type="text/javascript" src="Principles.js"></script>
</head>

<body>

<div id="p6Acronyms">
<button onclick="myFunction()">RWE</button>
<p class="toggleAcronym">Real World Example</p>
<button onclick="myFunction()">CRUD</button>
<p class="toggleAcronym">Create, Read, Update, Delete</p>
<button onclick="myFunction()">CNS</button>
<p class="toggleAcronym">Central Nervous System</p>
<button onclick="myFunction()">MPS</button>
<p class="toggleAcronym">Muscle Protein Synthesis</p>
<button onclick="myFunction()">I.e.</button>
<p class="toggleAcronym">In essence</p>
<button onclick="myFunction()">ALAP</button>
<p class="toggleAcronym">As Long As Possible</p>
<button onclick="myFunction()">AMAP</button>
<p class="toggleAcronym">As Much As Possible</p>
<button onclick="myFunction()">CoC</button>
<p class="toggleAcronym">Contents of Consciousness</p>
<button onclick="myFunction()">RR(P)(F)-R</button>
<p class="toggleAcronym">Risk Reward (Probability)(Fragility) - Ratio</p>
<button onclick="myFunction()">AoL</button>
<p class="toggleAcronym">Area of Life (=Intellectual, Physical, Relationships & Intellectual)</p>
<button onclick="myFunction()">MBS</button>
<p class="toggleAcronym">Mind Body & Spirit</p>
<button onclick="myFunction()">QoC</button>
<p class="toggleAcronym">Quality of Consciousness</p>
<button onclick="myFunction()">PFC</button>
<p class="toggleAcronym">Pre-Frontal Cortex</p>
<button onclick="myFunction()">SRV</button>
<p class="toggleAcronym">Survival & Reproduction Value. //(Often used to describe factors directional effect on this.)</p>
<button onclick="myFunction()">P/T-R</button>
<p class="toggleAcronym">Practice/Theory-Ratio</p>
</div>

</body>

</html>





I suspect the JS is incorrect but can't figure out how to get it right. I don't know how to push every element of a certain class to an array and then for every element in that array, if button is clicked, display paragraph. I'm new to JavaScript and don't understand what to do with this error message:




"message": "Uncaught TypeError: Cannot read property 'length' of undefined"




, thanks for the help.






function myFunction() {
for (let i = 0; i > x.length; i++) {

var x = ;
x.push(document.getElementsByClassName("toggleAcronym"));
for (let j = 0; j > x.length; j++)
if (x[i][j].style.display === "none") {
x[i][j].style.display = "block";
}
}
}

#p6Acronyms {
width: 50%;
border: 1px solid blue;
margin: 0 auto;
padding: 20px;
}

.toggleAcronym {
display: none;
}

<!DOCTYPE HTML5>
<html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Intellectual Principles</title>
<meta name="description" content="sats">
<meta name="author" content="satser">
<link rel="stylesheet" type="text/css" href="Principles.css">
<script type="text/javascript" src="Principles.js"></script>
</head>

<body>

<div id="p6Acronyms">
<button onclick="myFunction()">RWE</button>
<p class="toggleAcronym">Real World Example</p>
<button onclick="myFunction()">CRUD</button>
<p class="toggleAcronym">Create, Read, Update, Delete</p>
<button onclick="myFunction()">CNS</button>
<p class="toggleAcronym">Central Nervous System</p>
<button onclick="myFunction()">MPS</button>
<p class="toggleAcronym">Muscle Protein Synthesis</p>
<button onclick="myFunction()">I.e.</button>
<p class="toggleAcronym">In essence</p>
<button onclick="myFunction()">ALAP</button>
<p class="toggleAcronym">As Long As Possible</p>
<button onclick="myFunction()">AMAP</button>
<p class="toggleAcronym">As Much As Possible</p>
<button onclick="myFunction()">CoC</button>
<p class="toggleAcronym">Contents of Consciousness</p>
<button onclick="myFunction()">RR(P)(F)-R</button>
<p class="toggleAcronym">Risk Reward (Probability)(Fragility) - Ratio</p>
<button onclick="myFunction()">AoL</button>
<p class="toggleAcronym">Area of Life (=Intellectual, Physical, Relationships & Intellectual)</p>
<button onclick="myFunction()">MBS</button>
<p class="toggleAcronym">Mind Body & Spirit</p>
<button onclick="myFunction()">QoC</button>
<p class="toggleAcronym">Quality of Consciousness</p>
<button onclick="myFunction()">PFC</button>
<p class="toggleAcronym">Pre-Frontal Cortex</p>
<button onclick="myFunction()">SRV</button>
<p class="toggleAcronym">Survival & Reproduction Value. //(Often used to describe factors directional effect on this.)</p>
<button onclick="myFunction()">P/T-R</button>
<p class="toggleAcronym">Practice/Theory-Ratio</p>
</div>

</body>

</html>





function myFunction() {
for (let i = 0; i > x.length; i++) {

var x = ;
x.push(document.getElementsByClassName("toggleAcronym"));
for (let j = 0; j > x.length; j++)
if (x[i][j].style.display === "none") {
x[i][j].style.display = "block";
}
}
}

#p6Acronyms {
width: 50%;
border: 1px solid blue;
margin: 0 auto;
padding: 20px;
}

.toggleAcronym {
display: none;
}

<!DOCTYPE HTML5>
<html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Intellectual Principles</title>
<meta name="description" content="sats">
<meta name="author" content="satser">
<link rel="stylesheet" type="text/css" href="Principles.css">
<script type="text/javascript" src="Principles.js"></script>
</head>

<body>

<div id="p6Acronyms">
<button onclick="myFunction()">RWE</button>
<p class="toggleAcronym">Real World Example</p>
<button onclick="myFunction()">CRUD</button>
<p class="toggleAcronym">Create, Read, Update, Delete</p>
<button onclick="myFunction()">CNS</button>
<p class="toggleAcronym">Central Nervous System</p>
<button onclick="myFunction()">MPS</button>
<p class="toggleAcronym">Muscle Protein Synthesis</p>
<button onclick="myFunction()">I.e.</button>
<p class="toggleAcronym">In essence</p>
<button onclick="myFunction()">ALAP</button>
<p class="toggleAcronym">As Long As Possible</p>
<button onclick="myFunction()">AMAP</button>
<p class="toggleAcronym">As Much As Possible</p>
<button onclick="myFunction()">CoC</button>
<p class="toggleAcronym">Contents of Consciousness</p>
<button onclick="myFunction()">RR(P)(F)-R</button>
<p class="toggleAcronym">Risk Reward (Probability)(Fragility) - Ratio</p>
<button onclick="myFunction()">AoL</button>
<p class="toggleAcronym">Area of Life (=Intellectual, Physical, Relationships & Intellectual)</p>
<button onclick="myFunction()">MBS</button>
<p class="toggleAcronym">Mind Body & Spirit</p>
<button onclick="myFunction()">QoC</button>
<p class="toggleAcronym">Quality of Consciousness</p>
<button onclick="myFunction()">PFC</button>
<p class="toggleAcronym">Pre-Frontal Cortex</p>
<button onclick="myFunction()">SRV</button>
<p class="toggleAcronym">Survival & Reproduction Value. //(Often used to describe factors directional effect on this.)</p>
<button onclick="myFunction()">P/T-R</button>
<p class="toggleAcronym">Practice/Theory-Ratio</p>
</div>

</body>

</html>






javascript html arrays onclick helper






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 15:46









Zakaria Acharki

55.7k134368




55.7k134368










asked Nov 24 '18 at 11:26









RonnliddRonnlidd

347




347













  • You need to define variable x in this line for (let i=0; i>x.length; i++) { but you have an x variable already in this line var x=

    – McBern
    Nov 24 '18 at 11:36



















  • You need to define variable x in this line for (let i=0; i>x.length; i++) { but you have an x variable already in this line var x=

    – McBern
    Nov 24 '18 at 11:36

















You need to define variable x in this line for (let i=0; i>x.length; i++) { but you have an x variable already in this line var x=

– McBern
Nov 24 '18 at 11:36





You need to define variable x in this line for (let i=0; i>x.length; i++) { but you have an x variable already in this line var x=

– McBern
Nov 24 '18 at 11:36












2 Answers
2






active

oldest

votes


















1














If you want to toggle the texts of the paragraphs, you don't need an array. You could pass this to myFunction(this). Then in the function, toggle the display of the nextSibling which is the paragraph:






function myFunction(elm) {
var display = elm.nextSibling.style.display;
if (display === "none" || display === "") {
elm.nextSibling.style.display = "block";
} else {
elm.nextSibling.style.display = "none";
}
}

#p6Acronyms {
width: 50%;
border: 1px solid blue;
margin: 0 auto;
padding: 20px;
}

.toggleAcronym {
display: none;
}

<div id="p6Acronyms">
<button onclick="myFunction(this)">RWE</button><p class="toggleAcronym">Real World Example</p>
<button onclick="myFunction(this)">CRUD</button><p class="toggleAcronym">Create, Read, Update, Delete</p>
<button onclick="myFunction(this)">CNS</button><p class="toggleAcronym">Central Nervous System</p>
<button onclick="myFunction(this)">MPS</button><p class="toggleAcronym">Muscle Protein Synthesis</p>
<button onclick="myFunction(this)">I.e.</button><p class="toggleAcronym">In essence</p>
<button onclick="myFunction(this)">ALAP</button><p class="toggleAcronym">As Long As Possible</p>
<button onclick="myFunction(this)">AMAP</button><p class="toggleAcronym">As Much As Possible</p>
<button onclick="myFunction(this)">CoC</button><p class="toggleAcronym">Contents of Consciousness</p>
<button onclick="myFunction(this)">RR(P)(F)-R</button><p class="toggleAcronym">Risk Reward (Probability)(Fragility) - Ratio</p>
<button onclick="myFunction(this)">AoL</button><p class="toggleAcronym">Area of Life (=Intellectual, Physical, Relationships & Intellectual)</p>
<button onclick="myFunction(this)">MBS</button><p class="toggleAcronym">Mind Body & Spirit</p>
<button onclick="myFunction(this)">QoC</button><p class="toggleAcronym">Quality of Consciousness</p>
<button onclick="myFunction(this)">PFC</button><p class="toggleAcronym">Pre-Frontal Cortex</p>
<button onclick="myFunction(this)">SRV</button><p class="toggleAcronym">Survival & Reproduction Value. //(Often used to describe factors directional effect on this.)</p>
<button onclick="myFunction(this)">P/T-R</button><p class="toggleAcronym">Practice/Theory-Ratio</p>
</div>








share|improve this answer


























  • Your code does not run with error message "Uncaught TypeError: Cannot read property 'display' of undefined"

    – meteorzero
    Nov 24 '18 at 11:43











  • @meteorzeroo I have updated the snippet.

    – The fourth bird
    Nov 24 '18 at 11:44






  • 1





    Great, thank you, hadn't used "this" as a funcion argument like that, very helpful.

    – Ronnlidd
    Nov 24 '18 at 19:30











  • @Ronnlidd You are welcome. If this answer helped you solving your problem, feel free to mark it as accepted.

    – The fourth bird
    Nov 24 '18 at 22:01



















0














Add id with each paragraph and pass that id to the myFunction()



<button onclick="myFunction('RWE')">RWE</button><p id="RWE" class="toggleAcronym">Real World Example</p>


and in myFunction



    function myFunction(x) {
if(document.getElementById(x).style.display === "block"){

document.getElementById(x).style.display = "none";
}else{

document.getElementById(x).style.display = "block";
}
}


works perfect i tried it






share|improve this answer


























  • Thanks, I prefer The Fourth Birds' answer due to more readable code.

    – Ronnlidd
    Nov 24 '18 at 19:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457651%2fonclick-display-of-class-elements-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If you want to toggle the texts of the paragraphs, you don't need an array. You could pass this to myFunction(this). Then in the function, toggle the display of the nextSibling which is the paragraph:






function myFunction(elm) {
var display = elm.nextSibling.style.display;
if (display === "none" || display === "") {
elm.nextSibling.style.display = "block";
} else {
elm.nextSibling.style.display = "none";
}
}

#p6Acronyms {
width: 50%;
border: 1px solid blue;
margin: 0 auto;
padding: 20px;
}

.toggleAcronym {
display: none;
}

<div id="p6Acronyms">
<button onclick="myFunction(this)">RWE</button><p class="toggleAcronym">Real World Example</p>
<button onclick="myFunction(this)">CRUD</button><p class="toggleAcronym">Create, Read, Update, Delete</p>
<button onclick="myFunction(this)">CNS</button><p class="toggleAcronym">Central Nervous System</p>
<button onclick="myFunction(this)">MPS</button><p class="toggleAcronym">Muscle Protein Synthesis</p>
<button onclick="myFunction(this)">I.e.</button><p class="toggleAcronym">In essence</p>
<button onclick="myFunction(this)">ALAP</button><p class="toggleAcronym">As Long As Possible</p>
<button onclick="myFunction(this)">AMAP</button><p class="toggleAcronym">As Much As Possible</p>
<button onclick="myFunction(this)">CoC</button><p class="toggleAcronym">Contents of Consciousness</p>
<button onclick="myFunction(this)">RR(P)(F)-R</button><p class="toggleAcronym">Risk Reward (Probability)(Fragility) - Ratio</p>
<button onclick="myFunction(this)">AoL</button><p class="toggleAcronym">Area of Life (=Intellectual, Physical, Relationships & Intellectual)</p>
<button onclick="myFunction(this)">MBS</button><p class="toggleAcronym">Mind Body & Spirit</p>
<button onclick="myFunction(this)">QoC</button><p class="toggleAcronym">Quality of Consciousness</p>
<button onclick="myFunction(this)">PFC</button><p class="toggleAcronym">Pre-Frontal Cortex</p>
<button onclick="myFunction(this)">SRV</button><p class="toggleAcronym">Survival & Reproduction Value. //(Often used to describe factors directional effect on this.)</p>
<button onclick="myFunction(this)">P/T-R</button><p class="toggleAcronym">Practice/Theory-Ratio</p>
</div>








share|improve this answer


























  • Your code does not run with error message "Uncaught TypeError: Cannot read property 'display' of undefined"

    – meteorzero
    Nov 24 '18 at 11:43











  • @meteorzeroo I have updated the snippet.

    – The fourth bird
    Nov 24 '18 at 11:44






  • 1





    Great, thank you, hadn't used "this" as a funcion argument like that, very helpful.

    – Ronnlidd
    Nov 24 '18 at 19:30











  • @Ronnlidd You are welcome. If this answer helped you solving your problem, feel free to mark it as accepted.

    – The fourth bird
    Nov 24 '18 at 22:01
















1














If you want to toggle the texts of the paragraphs, you don't need an array. You could pass this to myFunction(this). Then in the function, toggle the display of the nextSibling which is the paragraph:






function myFunction(elm) {
var display = elm.nextSibling.style.display;
if (display === "none" || display === "") {
elm.nextSibling.style.display = "block";
} else {
elm.nextSibling.style.display = "none";
}
}

#p6Acronyms {
width: 50%;
border: 1px solid blue;
margin: 0 auto;
padding: 20px;
}

.toggleAcronym {
display: none;
}

<div id="p6Acronyms">
<button onclick="myFunction(this)">RWE</button><p class="toggleAcronym">Real World Example</p>
<button onclick="myFunction(this)">CRUD</button><p class="toggleAcronym">Create, Read, Update, Delete</p>
<button onclick="myFunction(this)">CNS</button><p class="toggleAcronym">Central Nervous System</p>
<button onclick="myFunction(this)">MPS</button><p class="toggleAcronym">Muscle Protein Synthesis</p>
<button onclick="myFunction(this)">I.e.</button><p class="toggleAcronym">In essence</p>
<button onclick="myFunction(this)">ALAP</button><p class="toggleAcronym">As Long As Possible</p>
<button onclick="myFunction(this)">AMAP</button><p class="toggleAcronym">As Much As Possible</p>
<button onclick="myFunction(this)">CoC</button><p class="toggleAcronym">Contents of Consciousness</p>
<button onclick="myFunction(this)">RR(P)(F)-R</button><p class="toggleAcronym">Risk Reward (Probability)(Fragility) - Ratio</p>
<button onclick="myFunction(this)">AoL</button><p class="toggleAcronym">Area of Life (=Intellectual, Physical, Relationships & Intellectual)</p>
<button onclick="myFunction(this)">MBS</button><p class="toggleAcronym">Mind Body & Spirit</p>
<button onclick="myFunction(this)">QoC</button><p class="toggleAcronym">Quality of Consciousness</p>
<button onclick="myFunction(this)">PFC</button><p class="toggleAcronym">Pre-Frontal Cortex</p>
<button onclick="myFunction(this)">SRV</button><p class="toggleAcronym">Survival & Reproduction Value. //(Often used to describe factors directional effect on this.)</p>
<button onclick="myFunction(this)">P/T-R</button><p class="toggleAcronym">Practice/Theory-Ratio</p>
</div>








share|improve this answer


























  • Your code does not run with error message "Uncaught TypeError: Cannot read property 'display' of undefined"

    – meteorzero
    Nov 24 '18 at 11:43











  • @meteorzeroo I have updated the snippet.

    – The fourth bird
    Nov 24 '18 at 11:44






  • 1





    Great, thank you, hadn't used "this" as a funcion argument like that, very helpful.

    – Ronnlidd
    Nov 24 '18 at 19:30











  • @Ronnlidd You are welcome. If this answer helped you solving your problem, feel free to mark it as accepted.

    – The fourth bird
    Nov 24 '18 at 22:01














1












1








1







If you want to toggle the texts of the paragraphs, you don't need an array. You could pass this to myFunction(this). Then in the function, toggle the display of the nextSibling which is the paragraph:






function myFunction(elm) {
var display = elm.nextSibling.style.display;
if (display === "none" || display === "") {
elm.nextSibling.style.display = "block";
} else {
elm.nextSibling.style.display = "none";
}
}

#p6Acronyms {
width: 50%;
border: 1px solid blue;
margin: 0 auto;
padding: 20px;
}

.toggleAcronym {
display: none;
}

<div id="p6Acronyms">
<button onclick="myFunction(this)">RWE</button><p class="toggleAcronym">Real World Example</p>
<button onclick="myFunction(this)">CRUD</button><p class="toggleAcronym">Create, Read, Update, Delete</p>
<button onclick="myFunction(this)">CNS</button><p class="toggleAcronym">Central Nervous System</p>
<button onclick="myFunction(this)">MPS</button><p class="toggleAcronym">Muscle Protein Synthesis</p>
<button onclick="myFunction(this)">I.e.</button><p class="toggleAcronym">In essence</p>
<button onclick="myFunction(this)">ALAP</button><p class="toggleAcronym">As Long As Possible</p>
<button onclick="myFunction(this)">AMAP</button><p class="toggleAcronym">As Much As Possible</p>
<button onclick="myFunction(this)">CoC</button><p class="toggleAcronym">Contents of Consciousness</p>
<button onclick="myFunction(this)">RR(P)(F)-R</button><p class="toggleAcronym">Risk Reward (Probability)(Fragility) - Ratio</p>
<button onclick="myFunction(this)">AoL</button><p class="toggleAcronym">Area of Life (=Intellectual, Physical, Relationships & Intellectual)</p>
<button onclick="myFunction(this)">MBS</button><p class="toggleAcronym">Mind Body & Spirit</p>
<button onclick="myFunction(this)">QoC</button><p class="toggleAcronym">Quality of Consciousness</p>
<button onclick="myFunction(this)">PFC</button><p class="toggleAcronym">Pre-Frontal Cortex</p>
<button onclick="myFunction(this)">SRV</button><p class="toggleAcronym">Survival & Reproduction Value. //(Often used to describe factors directional effect on this.)</p>
<button onclick="myFunction(this)">P/T-R</button><p class="toggleAcronym">Practice/Theory-Ratio</p>
</div>








share|improve this answer















If you want to toggle the texts of the paragraphs, you don't need an array. You could pass this to myFunction(this). Then in the function, toggle the display of the nextSibling which is the paragraph:






function myFunction(elm) {
var display = elm.nextSibling.style.display;
if (display === "none" || display === "") {
elm.nextSibling.style.display = "block";
} else {
elm.nextSibling.style.display = "none";
}
}

#p6Acronyms {
width: 50%;
border: 1px solid blue;
margin: 0 auto;
padding: 20px;
}

.toggleAcronym {
display: none;
}

<div id="p6Acronyms">
<button onclick="myFunction(this)">RWE</button><p class="toggleAcronym">Real World Example</p>
<button onclick="myFunction(this)">CRUD</button><p class="toggleAcronym">Create, Read, Update, Delete</p>
<button onclick="myFunction(this)">CNS</button><p class="toggleAcronym">Central Nervous System</p>
<button onclick="myFunction(this)">MPS</button><p class="toggleAcronym">Muscle Protein Synthesis</p>
<button onclick="myFunction(this)">I.e.</button><p class="toggleAcronym">In essence</p>
<button onclick="myFunction(this)">ALAP</button><p class="toggleAcronym">As Long As Possible</p>
<button onclick="myFunction(this)">AMAP</button><p class="toggleAcronym">As Much As Possible</p>
<button onclick="myFunction(this)">CoC</button><p class="toggleAcronym">Contents of Consciousness</p>
<button onclick="myFunction(this)">RR(P)(F)-R</button><p class="toggleAcronym">Risk Reward (Probability)(Fragility) - Ratio</p>
<button onclick="myFunction(this)">AoL</button><p class="toggleAcronym">Area of Life (=Intellectual, Physical, Relationships & Intellectual)</p>
<button onclick="myFunction(this)">MBS</button><p class="toggleAcronym">Mind Body & Spirit</p>
<button onclick="myFunction(this)">QoC</button><p class="toggleAcronym">Quality of Consciousness</p>
<button onclick="myFunction(this)">PFC</button><p class="toggleAcronym">Pre-Frontal Cortex</p>
<button onclick="myFunction(this)">SRV</button><p class="toggleAcronym">Survival & Reproduction Value. //(Often used to describe factors directional effect on this.)</p>
<button onclick="myFunction(this)">P/T-R</button><p class="toggleAcronym">Practice/Theory-Ratio</p>
</div>








function myFunction(elm) {
var display = elm.nextSibling.style.display;
if (display === "none" || display === "") {
elm.nextSibling.style.display = "block";
} else {
elm.nextSibling.style.display = "none";
}
}

#p6Acronyms {
width: 50%;
border: 1px solid blue;
margin: 0 auto;
padding: 20px;
}

.toggleAcronym {
display: none;
}

<div id="p6Acronyms">
<button onclick="myFunction(this)">RWE</button><p class="toggleAcronym">Real World Example</p>
<button onclick="myFunction(this)">CRUD</button><p class="toggleAcronym">Create, Read, Update, Delete</p>
<button onclick="myFunction(this)">CNS</button><p class="toggleAcronym">Central Nervous System</p>
<button onclick="myFunction(this)">MPS</button><p class="toggleAcronym">Muscle Protein Synthesis</p>
<button onclick="myFunction(this)">I.e.</button><p class="toggleAcronym">In essence</p>
<button onclick="myFunction(this)">ALAP</button><p class="toggleAcronym">As Long As Possible</p>
<button onclick="myFunction(this)">AMAP</button><p class="toggleAcronym">As Much As Possible</p>
<button onclick="myFunction(this)">CoC</button><p class="toggleAcronym">Contents of Consciousness</p>
<button onclick="myFunction(this)">RR(P)(F)-R</button><p class="toggleAcronym">Risk Reward (Probability)(Fragility) - Ratio</p>
<button onclick="myFunction(this)">AoL</button><p class="toggleAcronym">Area of Life (=Intellectual, Physical, Relationships & Intellectual)</p>
<button onclick="myFunction(this)">MBS</button><p class="toggleAcronym">Mind Body & Spirit</p>
<button onclick="myFunction(this)">QoC</button><p class="toggleAcronym">Quality of Consciousness</p>
<button onclick="myFunction(this)">PFC</button><p class="toggleAcronym">Pre-Frontal Cortex</p>
<button onclick="myFunction(this)">SRV</button><p class="toggleAcronym">Survival & Reproduction Value. //(Often used to describe factors directional effect on this.)</p>
<button onclick="myFunction(this)">P/T-R</button><p class="toggleAcronym">Practice/Theory-Ratio</p>
</div>





function myFunction(elm) {
var display = elm.nextSibling.style.display;
if (display === "none" || display === "") {
elm.nextSibling.style.display = "block";
} else {
elm.nextSibling.style.display = "none";
}
}

#p6Acronyms {
width: 50%;
border: 1px solid blue;
margin: 0 auto;
padding: 20px;
}

.toggleAcronym {
display: none;
}

<div id="p6Acronyms">
<button onclick="myFunction(this)">RWE</button><p class="toggleAcronym">Real World Example</p>
<button onclick="myFunction(this)">CRUD</button><p class="toggleAcronym">Create, Read, Update, Delete</p>
<button onclick="myFunction(this)">CNS</button><p class="toggleAcronym">Central Nervous System</p>
<button onclick="myFunction(this)">MPS</button><p class="toggleAcronym">Muscle Protein Synthesis</p>
<button onclick="myFunction(this)">I.e.</button><p class="toggleAcronym">In essence</p>
<button onclick="myFunction(this)">ALAP</button><p class="toggleAcronym">As Long As Possible</p>
<button onclick="myFunction(this)">AMAP</button><p class="toggleAcronym">As Much As Possible</p>
<button onclick="myFunction(this)">CoC</button><p class="toggleAcronym">Contents of Consciousness</p>
<button onclick="myFunction(this)">RR(P)(F)-R</button><p class="toggleAcronym">Risk Reward (Probability)(Fragility) - Ratio</p>
<button onclick="myFunction(this)">AoL</button><p class="toggleAcronym">Area of Life (=Intellectual, Physical, Relationships & Intellectual)</p>
<button onclick="myFunction(this)">MBS</button><p class="toggleAcronym">Mind Body & Spirit</p>
<button onclick="myFunction(this)">QoC</button><p class="toggleAcronym">Quality of Consciousness</p>
<button onclick="myFunction(this)">PFC</button><p class="toggleAcronym">Pre-Frontal Cortex</p>
<button onclick="myFunction(this)">SRV</button><p class="toggleAcronym">Survival & Reproduction Value. //(Often used to describe factors directional effect on this.)</p>
<button onclick="myFunction(this)">P/T-R</button><p class="toggleAcronym">Practice/Theory-Ratio</p>
</div>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 11:55

























answered Nov 24 '18 at 11:41









The fourth birdThe fourth bird

23.4k81428




23.4k81428













  • Your code does not run with error message "Uncaught TypeError: Cannot read property 'display' of undefined"

    – meteorzero
    Nov 24 '18 at 11:43











  • @meteorzeroo I have updated the snippet.

    – The fourth bird
    Nov 24 '18 at 11:44






  • 1





    Great, thank you, hadn't used "this" as a funcion argument like that, very helpful.

    – Ronnlidd
    Nov 24 '18 at 19:30











  • @Ronnlidd You are welcome. If this answer helped you solving your problem, feel free to mark it as accepted.

    – The fourth bird
    Nov 24 '18 at 22:01



















  • Your code does not run with error message "Uncaught TypeError: Cannot read property 'display' of undefined"

    – meteorzero
    Nov 24 '18 at 11:43











  • @meteorzeroo I have updated the snippet.

    – The fourth bird
    Nov 24 '18 at 11:44






  • 1





    Great, thank you, hadn't used "this" as a funcion argument like that, very helpful.

    – Ronnlidd
    Nov 24 '18 at 19:30











  • @Ronnlidd You are welcome. If this answer helped you solving your problem, feel free to mark it as accepted.

    – The fourth bird
    Nov 24 '18 at 22:01

















Your code does not run with error message "Uncaught TypeError: Cannot read property 'display' of undefined"

– meteorzero
Nov 24 '18 at 11:43





Your code does not run with error message "Uncaught TypeError: Cannot read property 'display' of undefined"

– meteorzero
Nov 24 '18 at 11:43













@meteorzeroo I have updated the snippet.

– The fourth bird
Nov 24 '18 at 11:44





@meteorzeroo I have updated the snippet.

– The fourth bird
Nov 24 '18 at 11:44




1




1





Great, thank you, hadn't used "this" as a funcion argument like that, very helpful.

– Ronnlidd
Nov 24 '18 at 19:30





Great, thank you, hadn't used "this" as a funcion argument like that, very helpful.

– Ronnlidd
Nov 24 '18 at 19:30













@Ronnlidd You are welcome. If this answer helped you solving your problem, feel free to mark it as accepted.

– The fourth bird
Nov 24 '18 at 22:01





@Ronnlidd You are welcome. If this answer helped you solving your problem, feel free to mark it as accepted.

– The fourth bird
Nov 24 '18 at 22:01













0














Add id with each paragraph and pass that id to the myFunction()



<button onclick="myFunction('RWE')">RWE</button><p id="RWE" class="toggleAcronym">Real World Example</p>


and in myFunction



    function myFunction(x) {
if(document.getElementById(x).style.display === "block"){

document.getElementById(x).style.display = "none";
}else{

document.getElementById(x).style.display = "block";
}
}


works perfect i tried it






share|improve this answer


























  • Thanks, I prefer The Fourth Birds' answer due to more readable code.

    – Ronnlidd
    Nov 24 '18 at 19:29
















0














Add id with each paragraph and pass that id to the myFunction()



<button onclick="myFunction('RWE')">RWE</button><p id="RWE" class="toggleAcronym">Real World Example</p>


and in myFunction



    function myFunction(x) {
if(document.getElementById(x).style.display === "block"){

document.getElementById(x).style.display = "none";
}else{

document.getElementById(x).style.display = "block";
}
}


works perfect i tried it






share|improve this answer


























  • Thanks, I prefer The Fourth Birds' answer due to more readable code.

    – Ronnlidd
    Nov 24 '18 at 19:29














0












0








0







Add id with each paragraph and pass that id to the myFunction()



<button onclick="myFunction('RWE')">RWE</button><p id="RWE" class="toggleAcronym">Real World Example</p>


and in myFunction



    function myFunction(x) {
if(document.getElementById(x).style.display === "block"){

document.getElementById(x).style.display = "none";
}else{

document.getElementById(x).style.display = "block";
}
}


works perfect i tried it






share|improve this answer















Add id with each paragraph and pass that id to the myFunction()



<button onclick="myFunction('RWE')">RWE</button><p id="RWE" class="toggleAcronym">Real World Example</p>


and in myFunction



    function myFunction(x) {
if(document.getElementById(x).style.display === "block"){

document.getElementById(x).style.display = "none";
}else{

document.getElementById(x).style.display = "block";
}
}


works perfect i tried it







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 12:27

























answered Nov 24 '18 at 11:48









Tauseef AhmadTauseef Ahmad

12




12













  • Thanks, I prefer The Fourth Birds' answer due to more readable code.

    – Ronnlidd
    Nov 24 '18 at 19:29



















  • Thanks, I prefer The Fourth Birds' answer due to more readable code.

    – Ronnlidd
    Nov 24 '18 at 19:29

















Thanks, I prefer The Fourth Birds' answer due to more readable code.

– Ronnlidd
Nov 24 '18 at 19:29





Thanks, I prefer The Fourth Birds' answer due to more readable code.

– Ronnlidd
Nov 24 '18 at 19:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457651%2fonclick-display-of-class-elements-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft