Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Cannot set property 'value' of null

Tags:

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 " when I tried to execute JS file..

Here is the HTMLcode:

<form action="">     <input type="text" class="searchbox1" name="search" placeholder="Search for Brand, Store or an Item..." value="text" />     <input type="button" class="searchbox_submit1" name="submit" value="text" onClick="javascript:getSearchText();"> </form> 

Here is the JS code:

function getSearchText() {     var searchText = document.getElementByName("search").value;     h_url=document.getElementById("u").value;     var theURL = h_url+'search_all/' + deptid + '/' + searchText + '/1';     $.ajax({         url : theURL,         fail: function(){         },         success : function() {         },         error:function(){         }     }); } 

Please help me to fix this.

like image 996
user2218532 Avatar asked Apr 19 '13 08:04

user2218532


People also ask

Can't set a property to null?

To solve the "Cannot set property of null" error, make sure that the DOM element you are accessing exists. The error is often thrown when trying to set a property after using the getElementById() method and passing it a non-existent id.

How do you fix TypeError Cannot read properties of null?

To solve the "Cannot read property 'value' of null" error, make sure you aren't accessing the value property on a null value, e.g. a non-existent DOM element. An element with the provided id does not exist in the DOM, so the getElementById method returns null .

Can't access property value document getElementById is null?

This error TypeError: document. getelementbyid(...) is null would seem to indicate that there is no such element with an ID passed to getElementById() exist. This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element.

Can not set properties of undefined?

The "Cannot set properties of undefined" error occurs when setting a property on an undefined value. To solve the error, conditionally check if the value is of the expected type (object or array) or has to be initialized before setting the property on it.


2 Answers

You don't have an element with the id u.That's why the error occurs. Note that you are trying to get the value of the input element with the name 'u' and it's not defined in your code.

like image 91
Deepu Avatar answered Oct 13 '22 00:10

Deepu


The problem may where the code is being executed. If you are in the head of a document executing JavaScript, even when you have an element with id="u" in your web page, the code gets executed before the DOM is finished loading, and so none of the HTML really exists yet... You can fix this by moving your code to the end of the page just above the closing html tag. This is one good reason to use jQuery.

like image 33
MJMacarty Avatar answered Oct 12 '22 23:10

MJMacarty