Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Adding Handlers to All Forms

const enableValidation = () => {
  // It will find all forms with the specified class in DOM, and
  // make an array from them using the Array.from() method
  const formList = Array.from(document.querySelectorAll(".form"));

  // Iterate over the resulting array
  formList.forEach((formElement) => {
    formElement.addEventListener("submit", (evt) => {
      // Cancel default behavior for each form
      evt.preventDefault();
    });

    // Call the setEventListeners() function for each form,
    // taking a form element as an argument
    setEventListeners(formElement);
  });
};

// Call the function
enableValidation(); 
Comment

Adding Handlers to All Forms

const setEventListeners = (formElement) => {
  const inputList = Array.from(formElement.querySelectorAll(".form__input"));
  inputList.forEach((inputElement) => {
    inputElement.addEventListener("input", function () {
      checkInputValidity(formElement, inputElement);
    });
  });
};


const enableValidation = () => {
  const formList = Array.from(document.querySelectorAll(".form"));
  formList.forEach((formElement) => {
    formElement.addEventListener("submit", (evt) => {
      evt.preventDefault();
    });
    setEventListeners(formElement);
  });
};

// Call the function
enableValidation();
Comment

PREVIOUS NEXT
Code Example
Javascript :: mreact graph 
Javascript :: full screen window open and disable all apps by javascript 
Javascript :: register js in viewyii2 
Javascript :: network information api js 
Javascript :: vite esbuild configuration 
Javascript :: save canvas from console 
Javascript :: app script with success handler response null 
Javascript :: "create a chatbot using javascript only" 
Javascript :: remember previous window javascript 
Javascript :: multi-line string shorthand javascript 
Javascript :: jquery swap table rows 
Javascript :: how to get min value from array of objects in javascript 
Javascript :: Node-Red: 2Outputs 
Javascript :: window.print specific div 
Javascript :: get current user moralis web3 login 
Javascript :: vue get key inside component 
Javascript :: gitignore jsconfig 
Javascript :: how to import kakao login page to navbar in react 
Javascript :: Javascript Class expressions 
Javascript :: 5.1.3. Boolean Expressions¶ 
Javascript :: Private slots are new and can be created via Private slot checks 
Javascript :: binary conversion recursion in javascript 
Javascript :: next-pwa push notification 
Javascript :: setinterval clearinterval querySelector until render 
Javascript :: api dfetch data in reactjs 
Javascript :: Example code of using inner blocks in Wordpress with ES5 
Javascript :: prevent the fire of parent event listener 
Javascript :: image support in node js chat app 
Javascript :: stiches js keyframes 
Javascript :: javascript string is mutable 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =