0

For the code below, I wanted to make the _formsOk function work for both Javascript arrays and "JQuery objects". In function1(), I tried to create a Javascript array with all DOM elements except those that have a parent element with id="objectTypesContainer". Basically, function1() filters out the DOM elements I don't want before calling _formsOk() function, which does the actual form validation.

function1() {
    var allForms = $('form:not(.vv_hidden)', this.selectMarketsContainer);
    var nonObjectTypeForms = [];
        allForms.each(function () {
            if ($(this).parent().attr("id") !== "objectTypesContainer"){
                nonObjectTypeForms.push($(this)[0]);
            }
        });

        return this._formsOk(nonObjectTypeForms);
},

_formsOk: function($forms) {
        var formOk = true;
        console.log(typeof $forms)
        $forms.each(function () {  // This line fails
            var validator = $(this).validate(DEFAULT_VALIDATION_OPTIONS);
            if (!(validator && validator.form())) {
                formOk = false;
            }
        });
        return formOk;
},

However, I realized that because nonObjectTypeForms is now a JS Array rather than a "JQuery Object", the line marked (// This line fails) now fails.

The original code looked like this:

function1() {
    var allForms = $('form:not(.vv_hidden)', this.selectMarketsContainer);  // This is a "JQuery object", so no error occurs
    return this._formsOk(allForms);
},

_formsOk: function($forms) {
        var formOk = true;
        console.log(typeof $forms)
        $forms.each(function () {  // This line fails
            var validator = $(this).validate(DEFAULT_VALIDATION_OPTIONS);
            if (!(validator && validator.form())) {
                formOk = false;
            }
        });
        return formOk;
},

Is there a way I can convert a JS array into a JQuery object ? I don't want to change _formsOk function definition just yet.

5
  • Doesn't $forms = $($forms); works? Commented Oct 10, 2019 at 18:07
  • 2
    how does it fail? Commented Oct 10, 2019 at 18:07
  • @Karl-AndréGagnon. That does work. Thanks for the suggestion. I'm a newbie at Javascript/JQuery. Commented Oct 10, 2019 at 18:12
  • @Neil. Some error is returned related to forms object not being JQuery object. Commented Oct 10, 2019 at 18:13
  • @JeroenHeier the line is shown in the code - it's $forms.each(). It would fail because $forms is a plain JS array, so it doesn't have a method called .each. Commented Oct 10, 2019 at 18:20

1 Answer 1

2

Instead of putting all elements in a new array, just use .filter() from the jQuery object.

allForms.filter(function () {
   return $(this).parent().attr("id") !== "objectTypesContainer")
});

This will remove all the items you don't need in your selection and now allForms will only have the wanted elements.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @VLAZ. That was a much cleaner solution for me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.