Home > database >  Can someone please assit me as to why this constant is returning null
Can someone please assit me as to why this constant is returning null

Time:11-02

the error message I keep getting Uncaught TypeError: form.querySelector(...) is null everything was working fine up until a while ago so i must of changed something that i dont know about file:///C:/Users/Ben/Documents/download text file/v12/v12 test .html:35

<html>
<form name="addtext">
<table>
<tr>
        <tr>
            <th> Fan Delay </th>
            <th><input id="fanDelay" type="text" name="fanDelay" value="0"   /></th>
        </tr>
        
</table>
    <table class="center">  
        <th></th>
        <th> <input type="submit" name="name" value="Save File" placeholder="File Name" /> </th>
    </table>
 </form>

</html>


<script>

/* Run script after DOMContentLoaded event to ensure form element is 
present */
document.addEventListener("DOMContentLoaded", function() {
  /* Obtain form element via querySelector */
  const form = document.querySelector('form[name="addtext"]');

  /* Bind listener to forms submit event */
  form.addEventListener("submit", function(event) {
    /* Prevent browsers default submit and page-reload behavior */
    event.preventDefault();

    
    var fanDelay   = form.querySelector('select[name="fanDelay"]').value;

        
    //
    const text =
`TACm Settings
VER
PRO
dt:  ${fanDelay}
    `;

    /* Create temporary link element and trigger file download  */
    const link = document.createElement("a");
    const href = "data:text/plain;charset=utf-8,"   encodeURIComponent(text);
    link.setAttribute("href", href);
    link.setAttribute("download", "usb.txt");

    document.body.appendChild(link);

    link.click();

    document.body.removeChild(link);
  });
});

</script>

CodePudding user response:

I think the error comes from here form.querySelector('select[name="fanDelay"]').value

You are using <input> tag, so just simply change that to this form.querySelector('input[name="fanDelay"]').value

CodePudding user response:

you trying to queryselector a select instead of input

try this :

var fanDelay   = form.querySelector('input[name="fanDelay"]').value;
  • Related