Uncaught TypeError: Cannot set property 'value' of null

前端 未结 8 927
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 07:58

I\'m trying to pass the entered text to the controller using an ajax request. But i\'m getting athe error \"Uncaught TypeError: Cannot set property \'value\' of null \" whe

相关标签:
8条回答
  • 2020-12-05 08:29

    It seems to be this function

    h_url=document.getElementById("u").value;
    

    You can help yourself using some 'console.log' to see what object is Null.

    0 讨论(0)
  • 2020-12-05 08:29

    guys This error because of Element Id not Visible from js Try to inspect element from UI and paste it on javascript file:

    before :

    document.getElementById('form:salesoverviewform:ticketstatusid').value =topping;
    

    After :

    document.getElementById('form:salesoverviewform:j_idt190:ticketstatusid').value =topping;
    

    Credits to Divya Akka .... :)

    0 讨论(0)
  • 2020-12-05 08:34

    I knew that i am too late for this answer, but i hope this will help to other who are facing and who will face.

    As you have written h_url is global var like var = h_url; so you can use that variable anywhere in your file.

    • h_url=document.getElementById("u").value; Here h_url contain value of your search box text value whatever user has typed.

    • document.getElementById("u"); This is the identifier of your form field with some specific ID.

    • Your Search Field without id

      <input type="text" class="searchbox1" name="search" placeholder="Search for Brand, Store or an Item..." value="text" />

    • Alter Search Field with id

      <input id="u" type="text" class="searchbox1" name="search" placeholder="Search for Brand, Store or an Item..." value="text" />

    • When you click on submit that will try to fetch value from document.getElementById("u").value; which is syntactically right but you haven't define id so that will return null.

    • So, Just make sure while you use form fields first define that ID and do other task letter.

    I hope this helps you and never get Cannot set property 'value' of null Error.

    0 讨论(0)
  • 2020-12-05 08:35

    The problem is that you haven't got any element with the id u so that you are calling something that doesn't exist.
    To fix that you have to add an id to the element.

    <input id="u" type="text" class="searchbox1" name="search" placeholder="Search for Brand, Store or an Item..." value="text" />
    


    And I've seen too you have added a value for the input, so it means the input is not empty and it will contain text. As result placeholder won't be displayed.

    Finally there is a warning that W3Validator will say because of the "/" in the end. :

    For the current document, the validator interprets strings like according to legacy rules that break the expectations of most authors and thus cause confusing warnings and error messages from the validator. This interpretation is triggered by HTML 4 documents or other SGML-based HTML documents. To avoid the messages, simply remove the "/" character in such contexts. NB: If you expect <FOO /> to be interpreted as an XML-compatible "self-closing" tag, then you need to use XHTML or HTML5.

    In conclusion it says you have to remove the slash. Simply write this:

    <input id="u" type="text" class="searchbox1" name="search" placeholder="Search for Brand, Store or an Item...">
    
    0 讨论(0)
  • 2020-12-05 08:35

    h_url=document.getElementById("u") is null here

    There is no element exist with id as u

    0 讨论(0)
  • 2020-12-05 08:39

    In case anyone landed on this page for a similar issue, I found that this error can happen if your JavaScript is running in the HEAD before your form is ready. Moving your JavaScript to the bottom of the page fixed it for my situation.

    0 讨论(0)
提交回复
热议问题