JavaScript toggle form elements
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
Fellow JS devs, I have come across a fairly simple question which has puzzled me. When the user submits a form I disable all inputs and submit button changes to a loading animation (fa-spinner
from fontawesome). Once the form submit is complete then I restore the previous state, and enable everything back.
My question comes to making this code more concise and deliberate in a simple toggle function:
function toggleFormElements(toggle, elArr)
if (toggle == 'disable')
elArr.btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
elArr.btn.setAttribute('disabled', 'disabled');
// inputs
elArr.fields.forEach((field, i) =>
field.setAttribute('disabled', 'disabled');
);
else if (toggle == 'enable')
elArr.btn.innerHTML = 'Submit';
elArr.btn.removeAttribute('disabled');
// inputs
elArr.fields.forEach((field, i) =>
field.removeAttribute('disabled');
);
Would it be possible to make this code more readable or simply shorter?
javascript
add a comment |Â
up vote
3
down vote
favorite
Fellow JS devs, I have come across a fairly simple question which has puzzled me. When the user submits a form I disable all inputs and submit button changes to a loading animation (fa-spinner
from fontawesome). Once the form submit is complete then I restore the previous state, and enable everything back.
My question comes to making this code more concise and deliberate in a simple toggle function:
function toggleFormElements(toggle, elArr)
if (toggle == 'disable')
elArr.btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
elArr.btn.setAttribute('disabled', 'disabled');
// inputs
elArr.fields.forEach((field, i) =>
field.setAttribute('disabled', 'disabled');
);
else if (toggle == 'enable')
elArr.btn.innerHTML = 'Submit';
elArr.btn.removeAttribute('disabled');
// inputs
elArr.fields.forEach((field, i) =>
field.removeAttribute('disabled');
);
Would it be possible to make this code more readable or simply shorter?
javascript
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Fellow JS devs, I have come across a fairly simple question which has puzzled me. When the user submits a form I disable all inputs and submit button changes to a loading animation (fa-spinner
from fontawesome). Once the form submit is complete then I restore the previous state, and enable everything back.
My question comes to making this code more concise and deliberate in a simple toggle function:
function toggleFormElements(toggle, elArr)
if (toggle == 'disable')
elArr.btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
elArr.btn.setAttribute('disabled', 'disabled');
// inputs
elArr.fields.forEach((field, i) =>
field.setAttribute('disabled', 'disabled');
);
else if (toggle == 'enable')
elArr.btn.innerHTML = 'Submit';
elArr.btn.removeAttribute('disabled');
// inputs
elArr.fields.forEach((field, i) =>
field.removeAttribute('disabled');
);
Would it be possible to make this code more readable or simply shorter?
javascript
Fellow JS devs, I have come across a fairly simple question which has puzzled me. When the user submits a form I disable all inputs and submit button changes to a loading animation (fa-spinner
from fontawesome). Once the form submit is complete then I restore the previous state, and enable everything back.
My question comes to making this code more concise and deliberate in a simple toggle function:
function toggleFormElements(toggle, elArr)
if (toggle == 'disable')
elArr.btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
elArr.btn.setAttribute('disabled', 'disabled');
// inputs
elArr.fields.forEach((field, i) =>
field.setAttribute('disabled', 'disabled');
);
else if (toggle == 'enable')
elArr.btn.innerHTML = 'Submit';
elArr.btn.removeAttribute('disabled');
// inputs
elArr.fields.forEach((field, i) =>
field.removeAttribute('disabled');
);
Would it be possible to make this code more readable or simply shorter?
javascript
asked May 8 at 18:59
Samuel
1639
1639
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
arr.forEach
I would consider using a simple for
loop to iterate over the elArr
(perhaps consider a bit better name, like elementsArray
or just elements
). This StackOverflow post goes over some of the benefits of doing this.
You can also use the disabled
attribute directly.
for (let i = 0; i < elArr.length; i++)
elArr[i].disabled = true; // or false to remove it
);
I would also personally prefer to have a boolean argument like isToggled
instead of expecting a string, so you can do something like below. In my opinion a boolean is less likely to result in an error (e.g. a misspelled string).
if (isToggled)
...
else
...
The comment // inputs
doesn't really achieve anything, I would suggest to remove it completely.
Haha yes the// inputs
comment was just for this question. I don't actually have it on there ðÂÂÂ
â Samuel
May 9 at 1:05
That's for the answer, it actually makes a lot of sense, especially with thebool
vs astring
â Samuel
May 9 at 1:06
add a comment |Â
up vote
3
down vote
That's not a toggle
- Toggle has a very specific meaning and comes with the benefit of not having to supply the new state when called. Your function is not a toggle and should be called something along the lines of
setFormDisableState(formElements, disable = true)
orsetFormState(formElements, disable)
Argument order
- If not using a default parameter use the right to left assignment rule when defining argument order. Eg assignment is right to left for
a = b
. Your function is assigning a state to elements in the objectelArr
, theelArr
should be to the left of thetoggle
.
Danger! implied type
elArr
is not an array. That is most defiantly a bad name as it implies the incorrect type.
The shortest form.
- For all attributes defined in the DOM set the values directly. You only use the attribute functions for attributes not defined by the DOM. Using the longer version to do anything in code adds unneeded noise.
Keep the source DRY
Don't Repeat Yourself (code). The field iterators
elArr.fields.forEach
are almost identical, with only the state changing.You have two if statements. You don't need to do the second test. If not disabling then must be enabling.
Alternatives
function toggleFormState(formDesc) // Desc is short for description
const newState = ! formDesc.btn.disabled;
formDesc.btn.innerHTML = newState ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
formDesc.btn.disabled = newState;
for (const element of formDesc.fields) element.disabled = newState
Or
function setFormState(formDesc, disable)
formDesc.btn.innerHTML = disable ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
formDesc.btn.disabled = disable;
for (const element of formDesc.fields) element.disabled = disable
nice use offor...of
in the alternatives
â Sam Onela
Jun 14 at 20:35
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
arr.forEach
I would consider using a simple for
loop to iterate over the elArr
(perhaps consider a bit better name, like elementsArray
or just elements
). This StackOverflow post goes over some of the benefits of doing this.
You can also use the disabled
attribute directly.
for (let i = 0; i < elArr.length; i++)
elArr[i].disabled = true; // or false to remove it
);
I would also personally prefer to have a boolean argument like isToggled
instead of expecting a string, so you can do something like below. In my opinion a boolean is less likely to result in an error (e.g. a misspelled string).
if (isToggled)
...
else
...
The comment // inputs
doesn't really achieve anything, I would suggest to remove it completely.
Haha yes the// inputs
comment was just for this question. I don't actually have it on there ðÂÂÂ
â Samuel
May 9 at 1:05
That's for the answer, it actually makes a lot of sense, especially with thebool
vs astring
â Samuel
May 9 at 1:06
add a comment |Â
up vote
2
down vote
accepted
arr.forEach
I would consider using a simple for
loop to iterate over the elArr
(perhaps consider a bit better name, like elementsArray
or just elements
). This StackOverflow post goes over some of the benefits of doing this.
You can also use the disabled
attribute directly.
for (let i = 0; i < elArr.length; i++)
elArr[i].disabled = true; // or false to remove it
);
I would also personally prefer to have a boolean argument like isToggled
instead of expecting a string, so you can do something like below. In my opinion a boolean is less likely to result in an error (e.g. a misspelled string).
if (isToggled)
...
else
...
The comment // inputs
doesn't really achieve anything, I would suggest to remove it completely.
Haha yes the// inputs
comment was just for this question. I don't actually have it on there ðÂÂÂ
â Samuel
May 9 at 1:05
That's for the answer, it actually makes a lot of sense, especially with thebool
vs astring
â Samuel
May 9 at 1:06
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
arr.forEach
I would consider using a simple for
loop to iterate over the elArr
(perhaps consider a bit better name, like elementsArray
or just elements
). This StackOverflow post goes over some of the benefits of doing this.
You can also use the disabled
attribute directly.
for (let i = 0; i < elArr.length; i++)
elArr[i].disabled = true; // or false to remove it
);
I would also personally prefer to have a boolean argument like isToggled
instead of expecting a string, so you can do something like below. In my opinion a boolean is less likely to result in an error (e.g. a misspelled string).
if (isToggled)
...
else
...
The comment // inputs
doesn't really achieve anything, I would suggest to remove it completely.
arr.forEach
I would consider using a simple for
loop to iterate over the elArr
(perhaps consider a bit better name, like elementsArray
or just elements
). This StackOverflow post goes over some of the benefits of doing this.
You can also use the disabled
attribute directly.
for (let i = 0; i < elArr.length; i++)
elArr[i].disabled = true; // or false to remove it
);
I would also personally prefer to have a boolean argument like isToggled
instead of expecting a string, so you can do something like below. In my opinion a boolean is less likely to result in an error (e.g. a misspelled string).
if (isToggled)
...
else
...
The comment // inputs
doesn't really achieve anything, I would suggest to remove it completely.
answered May 9 at 0:56
Phrancis
14.6k644137
14.6k644137
Haha yes the// inputs
comment was just for this question. I don't actually have it on there ðÂÂÂ
â Samuel
May 9 at 1:05
That's for the answer, it actually makes a lot of sense, especially with thebool
vs astring
â Samuel
May 9 at 1:06
add a comment |Â
Haha yes the// inputs
comment was just for this question. I don't actually have it on there ðÂÂÂ
â Samuel
May 9 at 1:05
That's for the answer, it actually makes a lot of sense, especially with thebool
vs astring
â Samuel
May 9 at 1:06
Haha yes the
// inputs
comment was just for this question. I don't actually have it on there ðÂÂÂâ Samuel
May 9 at 1:05
Haha yes the
// inputs
comment was just for this question. I don't actually have it on there ðÂÂÂâ Samuel
May 9 at 1:05
That's for the answer, it actually makes a lot of sense, especially with the
bool
vs a string
â Samuel
May 9 at 1:06
That's for the answer, it actually makes a lot of sense, especially with the
bool
vs a string
â Samuel
May 9 at 1:06
add a comment |Â
up vote
3
down vote
That's not a toggle
- Toggle has a very specific meaning and comes with the benefit of not having to supply the new state when called. Your function is not a toggle and should be called something along the lines of
setFormDisableState(formElements, disable = true)
orsetFormState(formElements, disable)
Argument order
- If not using a default parameter use the right to left assignment rule when defining argument order. Eg assignment is right to left for
a = b
. Your function is assigning a state to elements in the objectelArr
, theelArr
should be to the left of thetoggle
.
Danger! implied type
elArr
is not an array. That is most defiantly a bad name as it implies the incorrect type.
The shortest form.
- For all attributes defined in the DOM set the values directly. You only use the attribute functions for attributes not defined by the DOM. Using the longer version to do anything in code adds unneeded noise.
Keep the source DRY
Don't Repeat Yourself (code). The field iterators
elArr.fields.forEach
are almost identical, with only the state changing.You have two if statements. You don't need to do the second test. If not disabling then must be enabling.
Alternatives
function toggleFormState(formDesc) // Desc is short for description
const newState = ! formDesc.btn.disabled;
formDesc.btn.innerHTML = newState ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
formDesc.btn.disabled = newState;
for (const element of formDesc.fields) element.disabled = newState
Or
function setFormState(formDesc, disable)
formDesc.btn.innerHTML = disable ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
formDesc.btn.disabled = disable;
for (const element of formDesc.fields) element.disabled = disable
nice use offor...of
in the alternatives
â Sam Onela
Jun 14 at 20:35
add a comment |Â
up vote
3
down vote
That's not a toggle
- Toggle has a very specific meaning and comes with the benefit of not having to supply the new state when called. Your function is not a toggle and should be called something along the lines of
setFormDisableState(formElements, disable = true)
orsetFormState(formElements, disable)
Argument order
- If not using a default parameter use the right to left assignment rule when defining argument order. Eg assignment is right to left for
a = b
. Your function is assigning a state to elements in the objectelArr
, theelArr
should be to the left of thetoggle
.
Danger! implied type
elArr
is not an array. That is most defiantly a bad name as it implies the incorrect type.
The shortest form.
- For all attributes defined in the DOM set the values directly. You only use the attribute functions for attributes not defined by the DOM. Using the longer version to do anything in code adds unneeded noise.
Keep the source DRY
Don't Repeat Yourself (code). The field iterators
elArr.fields.forEach
are almost identical, with only the state changing.You have two if statements. You don't need to do the second test. If not disabling then must be enabling.
Alternatives
function toggleFormState(formDesc) // Desc is short for description
const newState = ! formDesc.btn.disabled;
formDesc.btn.innerHTML = newState ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
formDesc.btn.disabled = newState;
for (const element of formDesc.fields) element.disabled = newState
Or
function setFormState(formDesc, disable)
formDesc.btn.innerHTML = disable ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
formDesc.btn.disabled = disable;
for (const element of formDesc.fields) element.disabled = disable
nice use offor...of
in the alternatives
â Sam Onela
Jun 14 at 20:35
add a comment |Â
up vote
3
down vote
up vote
3
down vote
That's not a toggle
- Toggle has a very specific meaning and comes with the benefit of not having to supply the new state when called. Your function is not a toggle and should be called something along the lines of
setFormDisableState(formElements, disable = true)
orsetFormState(formElements, disable)
Argument order
- If not using a default parameter use the right to left assignment rule when defining argument order. Eg assignment is right to left for
a = b
. Your function is assigning a state to elements in the objectelArr
, theelArr
should be to the left of thetoggle
.
Danger! implied type
elArr
is not an array. That is most defiantly a bad name as it implies the incorrect type.
The shortest form.
- For all attributes defined in the DOM set the values directly. You only use the attribute functions for attributes not defined by the DOM. Using the longer version to do anything in code adds unneeded noise.
Keep the source DRY
Don't Repeat Yourself (code). The field iterators
elArr.fields.forEach
are almost identical, with only the state changing.You have two if statements. You don't need to do the second test. If not disabling then must be enabling.
Alternatives
function toggleFormState(formDesc) // Desc is short for description
const newState = ! formDesc.btn.disabled;
formDesc.btn.innerHTML = newState ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
formDesc.btn.disabled = newState;
for (const element of formDesc.fields) element.disabled = newState
Or
function setFormState(formDesc, disable)
formDesc.btn.innerHTML = disable ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
formDesc.btn.disabled = disable;
for (const element of formDesc.fields) element.disabled = disable
That's not a toggle
- Toggle has a very specific meaning and comes with the benefit of not having to supply the new state when called. Your function is not a toggle and should be called something along the lines of
setFormDisableState(formElements, disable = true)
orsetFormState(formElements, disable)
Argument order
- If not using a default parameter use the right to left assignment rule when defining argument order. Eg assignment is right to left for
a = b
. Your function is assigning a state to elements in the objectelArr
, theelArr
should be to the left of thetoggle
.
Danger! implied type
elArr
is not an array. That is most defiantly a bad name as it implies the incorrect type.
The shortest form.
- For all attributes defined in the DOM set the values directly. You only use the attribute functions for attributes not defined by the DOM. Using the longer version to do anything in code adds unneeded noise.
Keep the source DRY
Don't Repeat Yourself (code). The field iterators
elArr.fields.forEach
are almost identical, with only the state changing.You have two if statements. You don't need to do the second test. If not disabling then must be enabling.
Alternatives
function toggleFormState(formDesc) // Desc is short for description
const newState = ! formDesc.btn.disabled;
formDesc.btn.innerHTML = newState ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
formDesc.btn.disabled = newState;
for (const element of formDesc.fields) element.disabled = newState
Or
function setFormState(formDesc, disable)
formDesc.btn.innerHTML = disable ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
formDesc.btn.disabled = disable;
for (const element of formDesc.fields) element.disabled = disable
answered May 9 at 7:43
Blindman67
5,3111320
5,3111320
nice use offor...of
in the alternatives
â Sam Onela
Jun 14 at 20:35
add a comment |Â
nice use offor...of
in the alternatives
â Sam Onela
Jun 14 at 20:35
nice use of
for...of
in the alternativesâ Sam Onela
Jun 14 at 20:35
nice use of
for...of
in the alternativesâ Sam Onela
Jun 14 at 20:35
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%2f193946%2fjavascript-toggle-form-elements%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