Can't change innerHTML of child class for Read More/ Read Less Toggle in javascriptHow to change an element's class with JavaScript?How can I target a specific link/click in jQuery?Changing the title of an element using a child nodes innerHTMLCannot display HTML stringTruncate Text with JavaScript with “Read More” and “Read Less”Jquery Read More ToggleView More/Less toggle is being pushed down off pageonclick not working as expectedAdding read more toggle 2Accessing elements from HTML in JavaScript
In Endgame, wouldn't Stark have remembered Hulk busting out of the stairwell?
Count the number of triangles
How to handle inventory and story of a player leaving
How do you say "half the time …, the other half …" in German?
The meaning of asynchronous vs synchronous
Why is 3/4 a simple meter while 6/8 is a compound meter?
Does Mirrorwing Dragon's ability trigger when redirects are involved?
How to say "I only speak one language which is English" in French?
Why can't I identify major and minor chords?
What should be done with the carbon when using magic to get oxygen from carbon dioxide?
Why is "I let him to sleep" incorrect (or is it)?
What to do about my 1-month-old boy peeing through diapers?
Is the Amazon rainforest the "world's lungs"?
Why is there no Disney logo in MCU movies?
Should I judge the efficacy of Samadhi based on the ethical qualities of the meditator?
Why did the population of Bhutan drop by 70% between 2007 and 2008?
Why does a sticker slowly peel off, but if it is pulled quickly it tears?
Are spot colors limited and why CMYK mix is not treated same as spot color mix?
Why does AM radio react to IR remote?
Is allowing Barbarian features to work with Dex-based attacks imbalancing?
Cutting numbers into a specific decimals
What is the sound/audio equivalent of "unsightly"?
Shall I fix cracks on bathtub and how to fix them?
Did ancient peoples ever hide their treasure behind puzzles?
Can't change innerHTML of child class for Read More/ Read Less Toggle in javascript
How to change an element's class with JavaScript?How can I target a specific link/click in jQuery?Changing the title of an element using a child nodes innerHTMLCannot display HTML stringTruncate Text with JavaScript with “Read More” and “Read Less”Jquery Read More ToggleView More/Less toggle is being pushed down off pageonclick not working as expectedAdding read more toggle 2Accessing elements from HTML in JavaScript
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm building a IT website and I'm using an accordion to display my clients services. One clicks on the accordion and it expands or contracts giving a description of that service. I want to add a read more / read less text that changes depending on whether or not the accordion is open or closed (which is done using the is-active class. I can't change the innerHTML of the
Read More
text because it is the child element of the "accordion is-active" article.I've looked at many solutions and they don't seem to address the problem I'm facing because I need to access the child class of an element only IF that element has both the "accordion" and "is-active" classes. I've tried assigning the Read More tag with class = "readSwitch" id = "readSwitch" to a variable and changing that variables html e.g. z.innerHTML = "Read Less" but z is a copy of the y.getElementsByClass("readSwitch") not the actual element. How do I directly access the "readSwitch" class for elements that contain both "accordion is-active" classes?
JavaScript
function readToggle()
var x = document.getElementById("accordions");
var y = x.getElementsByClassName("accordion is-active");
var i;
for (i = 0; i < y.length; i++)
var z = y[i].getElementsByClassName("readSwitch");
z.innerHTML = "Read Less";
console.log(z.innerHTML);
HTML
<section class="accordions" id="accordions">
<article class="accordion is-active">
<div class="accordion-header toggle" onclick="readToggle()">
<p>
Custom Built Computers
</p>
<p style="font-size:10px" class="readSwitch"
id="readSwitch">
Read More
</p>
</div>
<div class="accordion-body">
<div class="accordion-content">
blah blah blah
</div>
</div>
</article>
</section>
The problem is z is a copy of y so changing it doesn't update the DOM. However when I try to directly access y's inner HTML using something like y[i].getElementsByClassName("readSwitch").innerHTML = "Read Less"; I get an error "TypeError: y.getElementsByClassName is not a function. (In 'y.getElementsByClassName("readSwitch")', 'y.getElementsByClassName' is undefined)" . Are there any ways to directly modify y's child classes innerHTML using something like y.readSwitch.innerHTML = "Read Less"?
EDIT SOLVED!!! :
Thanks to jonathan Heindl i was able to figure it out. I've been struggling with this stupid thing for 3 days and couldn't find any solutions on the internet so I'm posting my Javascript solution for the read more read less toggle here for others. This solution works with the HTML above.
document.onclick = function ()
//variables for accordions with is-active
var x = document.getElementById("accordions");
var y = x.getElementsByClassName("accordion is-active");
//variables for accordions with just accordion class, exclude those with "is-active"
var noless = document.querySelectorAll('article.accordion:not(.is-active)');
//Update all accordions with is-active to read less
var i;
for (i = 0; i < y.length; i++)
y[i].getElementsByClassName("readSwitch")[0].innerHTML = "Read Less ↑";
//update accordions without is-active class to read more
var w;
for (w = 0; w < noless.length; w++)
noless[w].getElementsByClassName("readSwitch")[0].innerHTML = "Read More ↓";
javascript jquery html bulma
add a comment |
I'm building a IT website and I'm using an accordion to display my clients services. One clicks on the accordion and it expands or contracts giving a description of that service. I want to add a read more / read less text that changes depending on whether or not the accordion is open or closed (which is done using the is-active class. I can't change the innerHTML of the
Read More
text because it is the child element of the "accordion is-active" article.I've looked at many solutions and they don't seem to address the problem I'm facing because I need to access the child class of an element only IF that element has both the "accordion" and "is-active" classes. I've tried assigning the Read More tag with class = "readSwitch" id = "readSwitch" to a variable and changing that variables html e.g. z.innerHTML = "Read Less" but z is a copy of the y.getElementsByClass("readSwitch") not the actual element. How do I directly access the "readSwitch" class for elements that contain both "accordion is-active" classes?
JavaScript
function readToggle()
var x = document.getElementById("accordions");
var y = x.getElementsByClassName("accordion is-active");
var i;
for (i = 0; i < y.length; i++)
var z = y[i].getElementsByClassName("readSwitch");
z.innerHTML = "Read Less";
console.log(z.innerHTML);
HTML
<section class="accordions" id="accordions">
<article class="accordion is-active">
<div class="accordion-header toggle" onclick="readToggle()">
<p>
Custom Built Computers
</p>
<p style="font-size:10px" class="readSwitch"
id="readSwitch">
Read More
</p>
</div>
<div class="accordion-body">
<div class="accordion-content">
blah blah blah
</div>
</div>
</article>
</section>
The problem is z is a copy of y so changing it doesn't update the DOM. However when I try to directly access y's inner HTML using something like y[i].getElementsByClassName("readSwitch").innerHTML = "Read Less"; I get an error "TypeError: y.getElementsByClassName is not a function. (In 'y.getElementsByClassName("readSwitch")', 'y.getElementsByClassName' is undefined)" . Are there any ways to directly modify y's child classes innerHTML using something like y.readSwitch.innerHTML = "Read Less"?
EDIT SOLVED!!! :
Thanks to jonathan Heindl i was able to figure it out. I've been struggling with this stupid thing for 3 days and couldn't find any solutions on the internet so I'm posting my Javascript solution for the read more read less toggle here for others. This solution works with the HTML above.
document.onclick = function ()
//variables for accordions with is-active
var x = document.getElementById("accordions");
var y = x.getElementsByClassName("accordion is-active");
//variables for accordions with just accordion class, exclude those with "is-active"
var noless = document.querySelectorAll('article.accordion:not(.is-active)');
//Update all accordions with is-active to read less
var i;
for (i = 0; i < y.length; i++)
y[i].getElementsByClassName("readSwitch")[0].innerHTML = "Read Less ↑";
//update accordions without is-active class to read more
var w;
for (w = 0; w < noless.length; w++)
noless[w].getElementsByClassName("readSwitch")[0].innerHTML = "Read More ↓";
javascript jquery html bulma
add a comment |
I'm building a IT website and I'm using an accordion to display my clients services. One clicks on the accordion and it expands or contracts giving a description of that service. I want to add a read more / read less text that changes depending on whether or not the accordion is open or closed (which is done using the is-active class. I can't change the innerHTML of the
Read More
text because it is the child element of the "accordion is-active" article.I've looked at many solutions and they don't seem to address the problem I'm facing because I need to access the child class of an element only IF that element has both the "accordion" and "is-active" classes. I've tried assigning the Read More tag with class = "readSwitch" id = "readSwitch" to a variable and changing that variables html e.g. z.innerHTML = "Read Less" but z is a copy of the y.getElementsByClass("readSwitch") not the actual element. How do I directly access the "readSwitch" class for elements that contain both "accordion is-active" classes?
JavaScript
function readToggle()
var x = document.getElementById("accordions");
var y = x.getElementsByClassName("accordion is-active");
var i;
for (i = 0; i < y.length; i++)
var z = y[i].getElementsByClassName("readSwitch");
z.innerHTML = "Read Less";
console.log(z.innerHTML);
HTML
<section class="accordions" id="accordions">
<article class="accordion is-active">
<div class="accordion-header toggle" onclick="readToggle()">
<p>
Custom Built Computers
</p>
<p style="font-size:10px" class="readSwitch"
id="readSwitch">
Read More
</p>
</div>
<div class="accordion-body">
<div class="accordion-content">
blah blah blah
</div>
</div>
</article>
</section>
The problem is z is a copy of y so changing it doesn't update the DOM. However when I try to directly access y's inner HTML using something like y[i].getElementsByClassName("readSwitch").innerHTML = "Read Less"; I get an error "TypeError: y.getElementsByClassName is not a function. (In 'y.getElementsByClassName("readSwitch")', 'y.getElementsByClassName' is undefined)" . Are there any ways to directly modify y's child classes innerHTML using something like y.readSwitch.innerHTML = "Read Less"?
EDIT SOLVED!!! :
Thanks to jonathan Heindl i was able to figure it out. I've been struggling with this stupid thing for 3 days and couldn't find any solutions on the internet so I'm posting my Javascript solution for the read more read less toggle here for others. This solution works with the HTML above.
document.onclick = function ()
//variables for accordions with is-active
var x = document.getElementById("accordions");
var y = x.getElementsByClassName("accordion is-active");
//variables for accordions with just accordion class, exclude those with "is-active"
var noless = document.querySelectorAll('article.accordion:not(.is-active)');
//Update all accordions with is-active to read less
var i;
for (i = 0; i < y.length; i++)
y[i].getElementsByClassName("readSwitch")[0].innerHTML = "Read Less ↑";
//update accordions without is-active class to read more
var w;
for (w = 0; w < noless.length; w++)
noless[w].getElementsByClassName("readSwitch")[0].innerHTML = "Read More ↓";
javascript jquery html bulma
I'm building a IT website and I'm using an accordion to display my clients services. One clicks on the accordion and it expands or contracts giving a description of that service. I want to add a read more / read less text that changes depending on whether or not the accordion is open or closed (which is done using the is-active class. I can't change the innerHTML of the
Read More
text because it is the child element of the "accordion is-active" article.I've looked at many solutions and they don't seem to address the problem I'm facing because I need to access the child class of an element only IF that element has both the "accordion" and "is-active" classes. I've tried assigning the Read More tag with class = "readSwitch" id = "readSwitch" to a variable and changing that variables html e.g. z.innerHTML = "Read Less" but z is a copy of the y.getElementsByClass("readSwitch") not the actual element. How do I directly access the "readSwitch" class for elements that contain both "accordion is-active" classes?
JavaScript
function readToggle()
var x = document.getElementById("accordions");
var y = x.getElementsByClassName("accordion is-active");
var i;
for (i = 0; i < y.length; i++)
var z = y[i].getElementsByClassName("readSwitch");
z.innerHTML = "Read Less";
console.log(z.innerHTML);
HTML
<section class="accordions" id="accordions">
<article class="accordion is-active">
<div class="accordion-header toggle" onclick="readToggle()">
<p>
Custom Built Computers
</p>
<p style="font-size:10px" class="readSwitch"
id="readSwitch">
Read More
</p>
</div>
<div class="accordion-body">
<div class="accordion-content">
blah blah blah
</div>
</div>
</article>
</section>
The problem is z is a copy of y so changing it doesn't update the DOM. However when I try to directly access y's inner HTML using something like y[i].getElementsByClassName("readSwitch").innerHTML = "Read Less"; I get an error "TypeError: y.getElementsByClassName is not a function. (In 'y.getElementsByClassName("readSwitch")', 'y.getElementsByClassName' is undefined)" . Are there any ways to directly modify y's child classes innerHTML using something like y.readSwitch.innerHTML = "Read Less"?
EDIT SOLVED!!! :
Thanks to jonathan Heindl i was able to figure it out. I've been struggling with this stupid thing for 3 days and couldn't find any solutions on the internet so I'm posting my Javascript solution for the read more read less toggle here for others. This solution works with the HTML above.
document.onclick = function ()
//variables for accordions with is-active
var x = document.getElementById("accordions");
var y = x.getElementsByClassName("accordion is-active");
//variables for accordions with just accordion class, exclude those with "is-active"
var noless = document.querySelectorAll('article.accordion:not(.is-active)');
//Update all accordions with is-active to read less
var i;
for (i = 0; i < y.length; i++)
y[i].getElementsByClassName("readSwitch")[0].innerHTML = "Read Less ↑";
//update accordions without is-active class to read more
var w;
for (w = 0; w < noless.length; w++)
noless[w].getElementsByClassName("readSwitch")[0].innerHTML = "Read More ↓";
javascript jquery html bulma
javascript jquery html bulma
edited Mar 27 at 21:53
Alder French
asked Mar 27 at 21:02
Alder FrenchAlder French
11 bronze badge
11 bronze badge
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
getElementsByClassName
returns an array of elements
so when you want to reference the object itself you need to add the index afterwards:
y[i].getElementsByClassName("readSwitch")[0].innerHTML
PS: When editing the text of a node it is recommended to use .textContent = "Read LEss"
otherwise you will overwrite all the childElements of said node
Bsp:
let x = document.getElementById("accordions");
let y = x.getElementsByClassName("accordion is-active");//maybe x.children depending on the structure
for (let i = 0; i < y.length; i++)
let z = y[i].getElementsByClassName("readSwitch")[0];
z.textContent= "Read Less";
console.log(z.textContent);
Thanks that solved it! Its always some silly little thing like that. Now I just have to figure out a way for it to update back to read more when the acccordion is compressed.
– Alder French
Mar 27 at 21:27
well you could also (probably should) update y[i].className="not-active" or similar then you can just do the same in reverse ( or store some variable on one of the elements to represent the state of clicked - that gets complicated real quick though
– jonathan Heindl
Mar 27 at 21:35
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55386406%2fcant-change-innerhtml-of-child-class-for-read-more-read-less-toggle-in-javascr%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
getElementsByClassName
returns an array of elements
so when you want to reference the object itself you need to add the index afterwards:
y[i].getElementsByClassName("readSwitch")[0].innerHTML
PS: When editing the text of a node it is recommended to use .textContent = "Read LEss"
otherwise you will overwrite all the childElements of said node
Bsp:
let x = document.getElementById("accordions");
let y = x.getElementsByClassName("accordion is-active");//maybe x.children depending on the structure
for (let i = 0; i < y.length; i++)
let z = y[i].getElementsByClassName("readSwitch")[0];
z.textContent= "Read Less";
console.log(z.textContent);
Thanks that solved it! Its always some silly little thing like that. Now I just have to figure out a way for it to update back to read more when the acccordion is compressed.
– Alder French
Mar 27 at 21:27
well you could also (probably should) update y[i].className="not-active" or similar then you can just do the same in reverse ( or store some variable on one of the elements to represent the state of clicked - that gets complicated real quick though
– jonathan Heindl
Mar 27 at 21:35
add a comment |
getElementsByClassName
returns an array of elements
so when you want to reference the object itself you need to add the index afterwards:
y[i].getElementsByClassName("readSwitch")[0].innerHTML
PS: When editing the text of a node it is recommended to use .textContent = "Read LEss"
otherwise you will overwrite all the childElements of said node
Bsp:
let x = document.getElementById("accordions");
let y = x.getElementsByClassName("accordion is-active");//maybe x.children depending on the structure
for (let i = 0; i < y.length; i++)
let z = y[i].getElementsByClassName("readSwitch")[0];
z.textContent= "Read Less";
console.log(z.textContent);
Thanks that solved it! Its always some silly little thing like that. Now I just have to figure out a way for it to update back to read more when the acccordion is compressed.
– Alder French
Mar 27 at 21:27
well you could also (probably should) update y[i].className="not-active" or similar then you can just do the same in reverse ( or store some variable on one of the elements to represent the state of clicked - that gets complicated real quick though
– jonathan Heindl
Mar 27 at 21:35
add a comment |
getElementsByClassName
returns an array of elements
so when you want to reference the object itself you need to add the index afterwards:
y[i].getElementsByClassName("readSwitch")[0].innerHTML
PS: When editing the text of a node it is recommended to use .textContent = "Read LEss"
otherwise you will overwrite all the childElements of said node
Bsp:
let x = document.getElementById("accordions");
let y = x.getElementsByClassName("accordion is-active");//maybe x.children depending on the structure
for (let i = 0; i < y.length; i++)
let z = y[i].getElementsByClassName("readSwitch")[0];
z.textContent= "Read Less";
console.log(z.textContent);
getElementsByClassName
returns an array of elements
so when you want to reference the object itself you need to add the index afterwards:
y[i].getElementsByClassName("readSwitch")[0].innerHTML
PS: When editing the text of a node it is recommended to use .textContent = "Read LEss"
otherwise you will overwrite all the childElements of said node
Bsp:
let x = document.getElementById("accordions");
let y = x.getElementsByClassName("accordion is-active");//maybe x.children depending on the structure
for (let i = 0; i < y.length; i++)
let z = y[i].getElementsByClassName("readSwitch")[0];
z.textContent= "Read Less";
console.log(z.textContent);
edited Mar 27 at 21:19
answered Mar 27 at 21:12
jonathan Heindljonathan Heindl
6342 silver badges13 bronze badges
6342 silver badges13 bronze badges
Thanks that solved it! Its always some silly little thing like that. Now I just have to figure out a way for it to update back to read more when the acccordion is compressed.
– Alder French
Mar 27 at 21:27
well you could also (probably should) update y[i].className="not-active" or similar then you can just do the same in reverse ( or store some variable on one of the elements to represent the state of clicked - that gets complicated real quick though
– jonathan Heindl
Mar 27 at 21:35
add a comment |
Thanks that solved it! Its always some silly little thing like that. Now I just have to figure out a way for it to update back to read more when the acccordion is compressed.
– Alder French
Mar 27 at 21:27
well you could also (probably should) update y[i].className="not-active" or similar then you can just do the same in reverse ( or store some variable on one of the elements to represent the state of clicked - that gets complicated real quick though
– jonathan Heindl
Mar 27 at 21:35
Thanks that solved it! Its always some silly little thing like that. Now I just have to figure out a way for it to update back to read more when the acccordion is compressed.
– Alder French
Mar 27 at 21:27
Thanks that solved it! Its always some silly little thing like that. Now I just have to figure out a way for it to update back to read more when the acccordion is compressed.
– Alder French
Mar 27 at 21:27
well you could also (probably should) update y[i].className="not-active" or similar then you can just do the same in reverse ( or store some variable on one of the elements to represent the state of clicked - that gets complicated real quick though
– jonathan Heindl
Mar 27 at 21:35
well you could also (probably should) update y[i].className="not-active" or similar then you can just do the same in reverse ( or store some variable on one of the elements to represent the state of clicked - that gets complicated real quick though
– jonathan Heindl
Mar 27 at 21:35
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55386406%2fcant-change-innerhtml-of-child-class-for-read-more-read-less-toggle-in-javascr%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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