How to make this Javascript function work without Enumerators?
MY PROBLEM
I have a Javascript function below where I am trying to modify the code so that the functionality works with Chome and other browsers. (PS: I found that Enumerator constructor is not a part of the ECMA standards, it's a Microsoft's JScript addition to the language. And to make my code work for latest browsers I need to use for..in loops to iterate objects )
MY CONDITION
I am a beginner to Javascript and I am finding a hard time figuring out how to make it work using for..in loops to iterate objects. I have 7 other functions like these but I'm sure if someone can help me with this one I will figure out the rest others on my own. Also, for..in is just 1 thing that has been advised if there are other efficient approaches to solve this case they are also most welcome.
MY JAVASCRIPT FUNCTION
Aside the comments that are present in the code I have put extra comments using /* */ so that you can see the points where objEnum has been used and accordingly advise what can be done. There are 3 places where it is in the code below.
function validateRequiredFieldsAllFieldsEnabledAndVisible(objFrm) {
var objElement;
var objEnum;
var blnError = false;
var blnIsChecked;
var objRadios;
//Gets Form contents collection
objEnum = new Enumerator(objFrm); /* 1st Occurence */
for(;!objEnum.atEnd();objEnum.moveNext()) /* 2nd Occurence */{
//Gets Next Element in collections
objElement = objEnum.item(); /* 3rd Occurence */
//Evaluates if the element is required
if(objElement.className == "Required") {
//Evaluates the Element's type
switch (objElement.tagName) {
case "SELECT":
if(objElement.value == "0" || objElement.value == "") //See if element has not been filled out
blnError = true;
break;
case "INPUT":
switch(objElement.type) {
case "radio":
blnIsChecked = false;
objRadios = document.getElementsByName(objElement.Name); //Gets the Collection of radio buttons
if(objRadios.length > 0)
{
//traverse the radio collection to see if there is one checked
for(var intCount=0;intCount<=objRadios.length - 1;intCount++) {
if(objRadios[intCount].checked) {
blnIsChecked = true;
break;
}
}
if(!blnIsChecked)
blnError = true;
}
break;
default:
if(trim(objElement.value) == "") //See if element has not been filled out
blnError = true;
}
break;
}
if(blnError) {
alert("Please Fill out the required fields.");
return false;
}
}
}
return true;
}
javascript
|
show 6 more comments
MY PROBLEM
I have a Javascript function below where I am trying to modify the code so that the functionality works with Chome and other browsers. (PS: I found that Enumerator constructor is not a part of the ECMA standards, it's a Microsoft's JScript addition to the language. And to make my code work for latest browsers I need to use for..in loops to iterate objects )
MY CONDITION
I am a beginner to Javascript and I am finding a hard time figuring out how to make it work using for..in loops to iterate objects. I have 7 other functions like these but I'm sure if someone can help me with this one I will figure out the rest others on my own. Also, for..in is just 1 thing that has been advised if there are other efficient approaches to solve this case they are also most welcome.
MY JAVASCRIPT FUNCTION
Aside the comments that are present in the code I have put extra comments using /* */ so that you can see the points where objEnum has been used and accordingly advise what can be done. There are 3 places where it is in the code below.
function validateRequiredFieldsAllFieldsEnabledAndVisible(objFrm) {
var objElement;
var objEnum;
var blnError = false;
var blnIsChecked;
var objRadios;
//Gets Form contents collection
objEnum = new Enumerator(objFrm); /* 1st Occurence */
for(;!objEnum.atEnd();objEnum.moveNext()) /* 2nd Occurence */{
//Gets Next Element in collections
objElement = objEnum.item(); /* 3rd Occurence */
//Evaluates if the element is required
if(objElement.className == "Required") {
//Evaluates the Element's type
switch (objElement.tagName) {
case "SELECT":
if(objElement.value == "0" || objElement.value == "") //See if element has not been filled out
blnError = true;
break;
case "INPUT":
switch(objElement.type) {
case "radio":
blnIsChecked = false;
objRadios = document.getElementsByName(objElement.Name); //Gets the Collection of radio buttons
if(objRadios.length > 0)
{
//traverse the radio collection to see if there is one checked
for(var intCount=0;intCount<=objRadios.length - 1;intCount++) {
if(objRadios[intCount].checked) {
blnIsChecked = true;
break;
}
}
if(!blnIsChecked)
blnError = true;
}
break;
default:
if(trim(objElement.value) == "") //See if element has not been filled out
blnError = true;
}
break;
}
if(blnError) {
alert("Please Fill out the required fields.");
return false;
}
}
}
return true;
}
javascript
Try to replace the enum instace withobjFrm.children, and use a regularfor` loop to iterate the collection. See developer.mozilla.org/en-US/docs/Web/API/ParentNode/children
– Teemu
Nov 27 '18 at 11:06
I will really appreciate if you can make the changes in the code and post your answer. I have no one to help me on this.
– Sagittarius009
Nov 27 '18 at 11:09
for (var n = 0; n < objFrm.children.length; n++) {objElement = objFrm[n]; /* The rest of the code as it is */ }. Notice also that JS is case-sensitive, should bedocument.getElementsByName(objElement.name). Have you translated this from VBScript?
– Teemu
Nov 27 '18 at 11:17
It would be useful to know what structure or type yourobjFrmis - is it a node/HTMLElement? Is it an array? An object?
– somethinghere
Nov 27 '18 at 11:19
@somethinghere I'd assume "//Gets Form contents collection" means a form element ..?
– Teemu
Nov 27 '18 at 11:21
|
show 6 more comments
MY PROBLEM
I have a Javascript function below where I am trying to modify the code so that the functionality works with Chome and other browsers. (PS: I found that Enumerator constructor is not a part of the ECMA standards, it's a Microsoft's JScript addition to the language. And to make my code work for latest browsers I need to use for..in loops to iterate objects )
MY CONDITION
I am a beginner to Javascript and I am finding a hard time figuring out how to make it work using for..in loops to iterate objects. I have 7 other functions like these but I'm sure if someone can help me with this one I will figure out the rest others on my own. Also, for..in is just 1 thing that has been advised if there are other efficient approaches to solve this case they are also most welcome.
MY JAVASCRIPT FUNCTION
Aside the comments that are present in the code I have put extra comments using /* */ so that you can see the points where objEnum has been used and accordingly advise what can be done. There are 3 places where it is in the code below.
function validateRequiredFieldsAllFieldsEnabledAndVisible(objFrm) {
var objElement;
var objEnum;
var blnError = false;
var blnIsChecked;
var objRadios;
//Gets Form contents collection
objEnum = new Enumerator(objFrm); /* 1st Occurence */
for(;!objEnum.atEnd();objEnum.moveNext()) /* 2nd Occurence */{
//Gets Next Element in collections
objElement = objEnum.item(); /* 3rd Occurence */
//Evaluates if the element is required
if(objElement.className == "Required") {
//Evaluates the Element's type
switch (objElement.tagName) {
case "SELECT":
if(objElement.value == "0" || objElement.value == "") //See if element has not been filled out
blnError = true;
break;
case "INPUT":
switch(objElement.type) {
case "radio":
blnIsChecked = false;
objRadios = document.getElementsByName(objElement.Name); //Gets the Collection of radio buttons
if(objRadios.length > 0)
{
//traverse the radio collection to see if there is one checked
for(var intCount=0;intCount<=objRadios.length - 1;intCount++) {
if(objRadios[intCount].checked) {
blnIsChecked = true;
break;
}
}
if(!blnIsChecked)
blnError = true;
}
break;
default:
if(trim(objElement.value) == "") //See if element has not been filled out
blnError = true;
}
break;
}
if(blnError) {
alert("Please Fill out the required fields.");
return false;
}
}
}
return true;
}
javascript
MY PROBLEM
I have a Javascript function below where I am trying to modify the code so that the functionality works with Chome and other browsers. (PS: I found that Enumerator constructor is not a part of the ECMA standards, it's a Microsoft's JScript addition to the language. And to make my code work for latest browsers I need to use for..in loops to iterate objects )
MY CONDITION
I am a beginner to Javascript and I am finding a hard time figuring out how to make it work using for..in loops to iterate objects. I have 7 other functions like these but I'm sure if someone can help me with this one I will figure out the rest others on my own. Also, for..in is just 1 thing that has been advised if there are other efficient approaches to solve this case they are also most welcome.
MY JAVASCRIPT FUNCTION
Aside the comments that are present in the code I have put extra comments using /* */ so that you can see the points where objEnum has been used and accordingly advise what can be done. There are 3 places where it is in the code below.
function validateRequiredFieldsAllFieldsEnabledAndVisible(objFrm) {
var objElement;
var objEnum;
var blnError = false;
var blnIsChecked;
var objRadios;
//Gets Form contents collection
objEnum = new Enumerator(objFrm); /* 1st Occurence */
for(;!objEnum.atEnd();objEnum.moveNext()) /* 2nd Occurence */{
//Gets Next Element in collections
objElement = objEnum.item(); /* 3rd Occurence */
//Evaluates if the element is required
if(objElement.className == "Required") {
//Evaluates the Element's type
switch (objElement.tagName) {
case "SELECT":
if(objElement.value == "0" || objElement.value == "") //See if element has not been filled out
blnError = true;
break;
case "INPUT":
switch(objElement.type) {
case "radio":
blnIsChecked = false;
objRadios = document.getElementsByName(objElement.Name); //Gets the Collection of radio buttons
if(objRadios.length > 0)
{
//traverse the radio collection to see if there is one checked
for(var intCount=0;intCount<=objRadios.length - 1;intCount++) {
if(objRadios[intCount].checked) {
blnIsChecked = true;
break;
}
}
if(!blnIsChecked)
blnError = true;
}
break;
default:
if(trim(objElement.value) == "") //See if element has not been filled out
blnError = true;
}
break;
}
if(blnError) {
alert("Please Fill out the required fields.");
return false;
}
}
}
return true;
}
javascript
javascript
asked Nov 27 '18 at 10:57
Sagittarius009Sagittarius009
1779
1779
Try to replace the enum instace withobjFrm.children, and use a regularfor` loop to iterate the collection. See developer.mozilla.org/en-US/docs/Web/API/ParentNode/children
– Teemu
Nov 27 '18 at 11:06
I will really appreciate if you can make the changes in the code and post your answer. I have no one to help me on this.
– Sagittarius009
Nov 27 '18 at 11:09
for (var n = 0; n < objFrm.children.length; n++) {objElement = objFrm[n]; /* The rest of the code as it is */ }. Notice also that JS is case-sensitive, should bedocument.getElementsByName(objElement.name). Have you translated this from VBScript?
– Teemu
Nov 27 '18 at 11:17
It would be useful to know what structure or type yourobjFrmis - is it a node/HTMLElement? Is it an array? An object?
– somethinghere
Nov 27 '18 at 11:19
@somethinghere I'd assume "//Gets Form contents collection" means a form element ..?
– Teemu
Nov 27 '18 at 11:21
|
show 6 more comments
Try to replace the enum instace withobjFrm.children, and use a regularfor` loop to iterate the collection. See developer.mozilla.org/en-US/docs/Web/API/ParentNode/children
– Teemu
Nov 27 '18 at 11:06
I will really appreciate if you can make the changes in the code and post your answer. I have no one to help me on this.
– Sagittarius009
Nov 27 '18 at 11:09
for (var n = 0; n < objFrm.children.length; n++) {objElement = objFrm[n]; /* The rest of the code as it is */ }. Notice also that JS is case-sensitive, should bedocument.getElementsByName(objElement.name). Have you translated this from VBScript?
– Teemu
Nov 27 '18 at 11:17
It would be useful to know what structure or type yourobjFrmis - is it a node/HTMLElement? Is it an array? An object?
– somethinghere
Nov 27 '18 at 11:19
@somethinghere I'd assume "//Gets Form contents collection" means a form element ..?
– Teemu
Nov 27 '18 at 11:21
Try to replace the enum instace with
objFrm.children, and use a regular for` loop to iterate the collection. See developer.mozilla.org/en-US/docs/Web/API/ParentNode/children– Teemu
Nov 27 '18 at 11:06
Try to replace the enum instace with
objFrm.children, and use a regular for` loop to iterate the collection. See developer.mozilla.org/en-US/docs/Web/API/ParentNode/children– Teemu
Nov 27 '18 at 11:06
I will really appreciate if you can make the changes in the code and post your answer. I have no one to help me on this.
– Sagittarius009
Nov 27 '18 at 11:09
I will really appreciate if you can make the changes in the code and post your answer. I have no one to help me on this.
– Sagittarius009
Nov 27 '18 at 11:09
for (var n = 0; n < objFrm.children.length; n++) {objElement = objFrm[n]; /* The rest of the code as it is */ }. Notice also that JS is case-sensitive, should be document.getElementsByName(objElement.name). Have you translated this from VBScript?– Teemu
Nov 27 '18 at 11:17
for (var n = 0; n < objFrm.children.length; n++) {objElement = objFrm[n]; /* The rest of the code as it is */ }. Notice also that JS is case-sensitive, should be document.getElementsByName(objElement.name). Have you translated this from VBScript?– Teemu
Nov 27 '18 at 11:17
It would be useful to know what structure or type your
objFrm is - is it a node/HTMLElement? Is it an array? An object?– somethinghere
Nov 27 '18 at 11:19
It would be useful to know what structure or type your
objFrm is - is it a node/HTMLElement? Is it an array? An object?– somethinghere
Nov 27 '18 at 11:19
@somethinghere I'd assume "//Gets Form contents collection" means a form element ..?
– Teemu
Nov 27 '18 at 11:21
@somethinghere I'd assume "//Gets Form contents collection" means a form element ..?
– Teemu
Nov 27 '18 at 11:21
|
show 6 more comments
0
active
oldest
votes
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%2f53498124%2fhow-to-make-this-javascript-function-work-without-enumerators%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53498124%2fhow-to-make-this-javascript-function-work-without-enumerators%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
Try to replace the enum instace with
objFrm.children, and use a regularfor` loop to iterate the collection. See developer.mozilla.org/en-US/docs/Web/API/ParentNode/children– Teemu
Nov 27 '18 at 11:06
I will really appreciate if you can make the changes in the code and post your answer. I have no one to help me on this.
– Sagittarius009
Nov 27 '18 at 11:09
for (var n = 0; n < objFrm.children.length; n++) {objElement = objFrm[n]; /* The rest of the code as it is */ }. Notice also that JS is case-sensitive, should bedocument.getElementsByName(objElement.name). Have you translated this from VBScript?– Teemu
Nov 27 '18 at 11:17
It would be useful to know what structure or type your
objFrmis - is it a node/HTMLElement? Is it an array? An object?– somethinghere
Nov 27 '18 at 11:19
@somethinghere I'd assume "//Gets Form contents collection" means a form element ..?
– Teemu
Nov 27 '18 at 11:21