Disable Submit for Empty Required
JavaScript
Easy
3 views
Problem Description
You have a form #f with inputs having attribute data-req=1. Disable button #submit until all required inputs have some value (trimmed).
Output Format
No output (DOM change).
Constraints
Assume #f form and #submit button exist. Required inputs have data-req=1.
Official Solution
const form=document.getElementById('f');const submit=document.getElementById('submit');const req=[...form.querySelectorAll(`[data-req='1']`)];const ok=()=>req.every(el=>String(el.value||'').trim().length>0);const upd=()=>{submit.disabled=!ok();};form.addEventListener('input',upd);upd();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!