Buttons that expand or collapse all the within the document
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I have <button>
s that when pressed expand or collapse the contents of all the <details>
within the document. The code works, but as a novice in JavaScript, I am not very confident in its performance and/or efficiency. Could you please review it.
expandDetBtn = document.getElementById("showBtn");
collapseDetBtn = document.getElementById("hideBtn");
expandDetBtn.addEventListener("click", function expandDetails(x)
var x = document.getElementsByTagName("details");
var i;
var len = x.length;
for (i = 0; i < len; i++)
if (x[i].open == false)
x[i].setAttribute("open", "true");
);
collapseDetBtn.addEventListener("click", function expandDetails(x)
var x = document.getElementsByTagName("details");
var i;
var len = x.length;
for (i = 0; i < len; i++)
if (x[i].open)
x[i].removeAttribute("open");
);
<button id="showBtn">Show details</button>
<button id="hideBtn">Hide details</button>
<details>
<summary>Section 1</summary>
<p>Text 1</p>
</details>
<details>
<summary>Section 2</summary>
<p>Text 2</p>
</details>
<details>
<summary>Section 3</summary>
<p>Text 3</p>
</details>
javascript performance beginner
add a comment |Â
up vote
0
down vote
favorite
I have <button>
s that when pressed expand or collapse the contents of all the <details>
within the document. The code works, but as a novice in JavaScript, I am not very confident in its performance and/or efficiency. Could you please review it.
expandDetBtn = document.getElementById("showBtn");
collapseDetBtn = document.getElementById("hideBtn");
expandDetBtn.addEventListener("click", function expandDetails(x)
var x = document.getElementsByTagName("details");
var i;
var len = x.length;
for (i = 0; i < len; i++)
if (x[i].open == false)
x[i].setAttribute("open", "true");
);
collapseDetBtn.addEventListener("click", function expandDetails(x)
var x = document.getElementsByTagName("details");
var i;
var len = x.length;
for (i = 0; i < len; i++)
if (x[i].open)
x[i].removeAttribute("open");
);
<button id="showBtn">Show details</button>
<button id="hideBtn">Hide details</button>
<details>
<summary>Section 1</summary>
<p>Text 1</p>
</details>
<details>
<summary>Section 2</summary>
<p>Text 2</p>
</details>
<details>
<summary>Section 3</summary>
<p>Text 3</p>
</details>
javascript performance beginner
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have <button>
s that when pressed expand or collapse the contents of all the <details>
within the document. The code works, but as a novice in JavaScript, I am not very confident in its performance and/or efficiency. Could you please review it.
expandDetBtn = document.getElementById("showBtn");
collapseDetBtn = document.getElementById("hideBtn");
expandDetBtn.addEventListener("click", function expandDetails(x)
var x = document.getElementsByTagName("details");
var i;
var len = x.length;
for (i = 0; i < len; i++)
if (x[i].open == false)
x[i].setAttribute("open", "true");
);
collapseDetBtn.addEventListener("click", function expandDetails(x)
var x = document.getElementsByTagName("details");
var i;
var len = x.length;
for (i = 0; i < len; i++)
if (x[i].open)
x[i].removeAttribute("open");
);
<button id="showBtn">Show details</button>
<button id="hideBtn">Hide details</button>
<details>
<summary>Section 1</summary>
<p>Text 1</p>
</details>
<details>
<summary>Section 2</summary>
<p>Text 2</p>
</details>
<details>
<summary>Section 3</summary>
<p>Text 3</p>
</details>
javascript performance beginner
I have <button>
s that when pressed expand or collapse the contents of all the <details>
within the document. The code works, but as a novice in JavaScript, I am not very confident in its performance and/or efficiency. Could you please review it.
expandDetBtn = document.getElementById("showBtn");
collapseDetBtn = document.getElementById("hideBtn");
expandDetBtn.addEventListener("click", function expandDetails(x)
var x = document.getElementsByTagName("details");
var i;
var len = x.length;
for (i = 0; i < len; i++)
if (x[i].open == false)
x[i].setAttribute("open", "true");
);
collapseDetBtn.addEventListener("click", function expandDetails(x)
var x = document.getElementsByTagName("details");
var i;
var len = x.length;
for (i = 0; i < len; i++)
if (x[i].open)
x[i].removeAttribute("open");
);
<button id="showBtn">Show details</button>
<button id="hideBtn">Hide details</button>
<details>
<summary>Section 1</summary>
<p>Text 1</p>
</details>
<details>
<summary>Section 2</summary>
<p>Text 2</p>
</details>
<details>
<summary>Section 3</summary>
<p>Text 3</p>
</details>
expandDetBtn = document.getElementById("showBtn");
collapseDetBtn = document.getElementById("hideBtn");
expandDetBtn.addEventListener("click", function expandDetails(x)
var x = document.getElementsByTagName("details");
var i;
var len = x.length;
for (i = 0; i < len; i++)
if (x[i].open == false)
x[i].setAttribute("open", "true");
);
collapseDetBtn.addEventListener("click", function expandDetails(x)
var x = document.getElementsByTagName("details");
var i;
var len = x.length;
for (i = 0; i < len; i++)
if (x[i].open)
x[i].removeAttribute("open");
);
<button id="showBtn">Show details</button>
<button id="hideBtn">Hide details</button>
<details>
<summary>Section 1</summary>
<p>Text 1</p>
</details>
<details>
<summary>Section 2</summary>
<p>Text 2</p>
</details>
<details>
<summary>Section 3</summary>
<p>Text 3</p>
</details>
expandDetBtn = document.getElementById("showBtn");
collapseDetBtn = document.getElementById("hideBtn");
expandDetBtn.addEventListener("click", function expandDetails(x)
var x = document.getElementsByTagName("details");
var i;
var len = x.length;
for (i = 0; i < len; i++)
if (x[i].open == false)
x[i].setAttribute("open", "true");
);
collapseDetBtn.addEventListener("click", function expandDetails(x)
var x = document.getElementsByTagName("details");
var i;
var len = x.length;
for (i = 0; i < len; i++)
if (x[i].open)
x[i].removeAttribute("open");
);
<button id="showBtn">Show details</button>
<button id="hideBtn">Hide details</button>
<details>
<summary>Section 1</summary>
<p>Text 1</p>
</details>
<details>
<summary>Section 2</summary>
<p>Text 2</p>
</details>
<details>
<summary>Section 3</summary>
<p>Text 3</p>
</details>
javascript performance beginner
asked Apr 15 at 19:50
JAT86
1224
1224
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Performance-wise the code is fine. There is not really anything to improve. I have a few other comments about the code.
You are accessing DOM elements at the start, without checking if they are loaded. Usually they are not, which would cause an error here.
You are naming your handler function, but never refer to them. You can just use an anonymous function here. You also don't use the x
parameter.
Don't use var
, use let
and const
instead.
x
is not a very descriptive name. It also overwrites the exiting parameter.
There is no need to save the length in a variable. You also don't need to declare i
before the loop, since it's not used outside.
There is no need to check if they are already opened or closed. Opening one which is already open doesn't do anything, so just open them all.
Don't use "true"
here .setAttribute("open", "true");
. According to this.
Boolean attributes are considered to be true if they're present on the
element at all, regardless of their actual value; as a rule, you
should specify the empty string ("") in value
The outcome is the same, but it might lead people to believe that setting the value to "false"
will remove it, which is not the case.
With all these changes, the opening look like this
expandDetBtn.addEventListener("click", function()
const elements = document.getElementsByTagName("details");
for (let i = 0; i < elements.length; i++)
elements[i].setAttribute("open", "");
);
If you want to get more advanced you could use a single function, since they are very similar.
function toggleDetails(open)
for(element of document.getElementsByTagName("details"))
element.open = open;
expandDetBtn.addEventListener("click", toggleDetails.bind(null, true));
collapseDetBtn.addEventListener("click", toggleDetails.bind(null, false));
<detail>
elements have anopen
property, so it should really just beelement.open = open
â Gerrit0
Apr 16 at 14:58
@Gerrit0 you are right, didn't even think about that. Thanks.
â Kruga
Apr 16 at 15:23
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Performance-wise the code is fine. There is not really anything to improve. I have a few other comments about the code.
You are accessing DOM elements at the start, without checking if they are loaded. Usually they are not, which would cause an error here.
You are naming your handler function, but never refer to them. You can just use an anonymous function here. You also don't use the x
parameter.
Don't use var
, use let
and const
instead.
x
is not a very descriptive name. It also overwrites the exiting parameter.
There is no need to save the length in a variable. You also don't need to declare i
before the loop, since it's not used outside.
There is no need to check if they are already opened or closed. Opening one which is already open doesn't do anything, so just open them all.
Don't use "true"
here .setAttribute("open", "true");
. According to this.
Boolean attributes are considered to be true if they're present on the
element at all, regardless of their actual value; as a rule, you
should specify the empty string ("") in value
The outcome is the same, but it might lead people to believe that setting the value to "false"
will remove it, which is not the case.
With all these changes, the opening look like this
expandDetBtn.addEventListener("click", function()
const elements = document.getElementsByTagName("details");
for (let i = 0; i < elements.length; i++)
elements[i].setAttribute("open", "");
);
If you want to get more advanced you could use a single function, since they are very similar.
function toggleDetails(open)
for(element of document.getElementsByTagName("details"))
element.open = open;
expandDetBtn.addEventListener("click", toggleDetails.bind(null, true));
collapseDetBtn.addEventListener("click", toggleDetails.bind(null, false));
<detail>
elements have anopen
property, so it should really just beelement.open = open
â Gerrit0
Apr 16 at 14:58
@Gerrit0 you are right, didn't even think about that. Thanks.
â Kruga
Apr 16 at 15:23
add a comment |Â
up vote
1
down vote
accepted
Performance-wise the code is fine. There is not really anything to improve. I have a few other comments about the code.
You are accessing DOM elements at the start, without checking if they are loaded. Usually they are not, which would cause an error here.
You are naming your handler function, but never refer to them. You can just use an anonymous function here. You also don't use the x
parameter.
Don't use var
, use let
and const
instead.
x
is not a very descriptive name. It also overwrites the exiting parameter.
There is no need to save the length in a variable. You also don't need to declare i
before the loop, since it's not used outside.
There is no need to check if they are already opened or closed. Opening one which is already open doesn't do anything, so just open them all.
Don't use "true"
here .setAttribute("open", "true");
. According to this.
Boolean attributes are considered to be true if they're present on the
element at all, regardless of their actual value; as a rule, you
should specify the empty string ("") in value
The outcome is the same, but it might lead people to believe that setting the value to "false"
will remove it, which is not the case.
With all these changes, the opening look like this
expandDetBtn.addEventListener("click", function()
const elements = document.getElementsByTagName("details");
for (let i = 0; i < elements.length; i++)
elements[i].setAttribute("open", "");
);
If you want to get more advanced you could use a single function, since they are very similar.
function toggleDetails(open)
for(element of document.getElementsByTagName("details"))
element.open = open;
expandDetBtn.addEventListener("click", toggleDetails.bind(null, true));
collapseDetBtn.addEventListener("click", toggleDetails.bind(null, false));
<detail>
elements have anopen
property, so it should really just beelement.open = open
â Gerrit0
Apr 16 at 14:58
@Gerrit0 you are right, didn't even think about that. Thanks.
â Kruga
Apr 16 at 15:23
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Performance-wise the code is fine. There is not really anything to improve. I have a few other comments about the code.
You are accessing DOM elements at the start, without checking if they are loaded. Usually they are not, which would cause an error here.
You are naming your handler function, but never refer to them. You can just use an anonymous function here. You also don't use the x
parameter.
Don't use var
, use let
and const
instead.
x
is not a very descriptive name. It also overwrites the exiting parameter.
There is no need to save the length in a variable. You also don't need to declare i
before the loop, since it's not used outside.
There is no need to check if they are already opened or closed. Opening one which is already open doesn't do anything, so just open them all.
Don't use "true"
here .setAttribute("open", "true");
. According to this.
Boolean attributes are considered to be true if they're present on the
element at all, regardless of their actual value; as a rule, you
should specify the empty string ("") in value
The outcome is the same, but it might lead people to believe that setting the value to "false"
will remove it, which is not the case.
With all these changes, the opening look like this
expandDetBtn.addEventListener("click", function()
const elements = document.getElementsByTagName("details");
for (let i = 0; i < elements.length; i++)
elements[i].setAttribute("open", "");
);
If you want to get more advanced you could use a single function, since they are very similar.
function toggleDetails(open)
for(element of document.getElementsByTagName("details"))
element.open = open;
expandDetBtn.addEventListener("click", toggleDetails.bind(null, true));
collapseDetBtn.addEventListener("click", toggleDetails.bind(null, false));
Performance-wise the code is fine. There is not really anything to improve. I have a few other comments about the code.
You are accessing DOM elements at the start, without checking if they are loaded. Usually they are not, which would cause an error here.
You are naming your handler function, but never refer to them. You can just use an anonymous function here. You also don't use the x
parameter.
Don't use var
, use let
and const
instead.
x
is not a very descriptive name. It also overwrites the exiting parameter.
There is no need to save the length in a variable. You also don't need to declare i
before the loop, since it's not used outside.
There is no need to check if they are already opened or closed. Opening one which is already open doesn't do anything, so just open them all.
Don't use "true"
here .setAttribute("open", "true");
. According to this.
Boolean attributes are considered to be true if they're present on the
element at all, regardless of their actual value; as a rule, you
should specify the empty string ("") in value
The outcome is the same, but it might lead people to believe that setting the value to "false"
will remove it, which is not the case.
With all these changes, the opening look like this
expandDetBtn.addEventListener("click", function()
const elements = document.getElementsByTagName("details");
for (let i = 0; i < elements.length; i++)
elements[i].setAttribute("open", "");
);
If you want to get more advanced you could use a single function, since they are very similar.
function toggleDetails(open)
for(element of document.getElementsByTagName("details"))
element.open = open;
expandDetBtn.addEventListener("click", toggleDetails.bind(null, true));
collapseDetBtn.addEventListener("click", toggleDetails.bind(null, false));
edited Apr 16 at 15:20
answered Apr 16 at 13:14
Kruga
74819
74819
<detail>
elements have anopen
property, so it should really just beelement.open = open
â Gerrit0
Apr 16 at 14:58
@Gerrit0 you are right, didn't even think about that. Thanks.
â Kruga
Apr 16 at 15:23
add a comment |Â
<detail>
elements have anopen
property, so it should really just beelement.open = open
â Gerrit0
Apr 16 at 14:58
@Gerrit0 you are right, didn't even think about that. Thanks.
â Kruga
Apr 16 at 15:23
<detail>
elements have an open
property, so it should really just be element.open = open
â Gerrit0
Apr 16 at 14:58
<detail>
elements have an open
property, so it should really just be element.open = open
â Gerrit0
Apr 16 at 14:58
@Gerrit0 you are right, didn't even think about that. Thanks.
â Kruga
Apr 16 at 15:23
@Gerrit0 you are right, didn't even think about that. Thanks.
â Kruga
Apr 16 at 15:23
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192138%2fbuttons-that-expand-or-collapse-all-the-details-within-the-document%23new-answer', 'question_page');
);
Post as a guest
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
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
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