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.
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.
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. :
In conclusion it says you have to remove the slash. Simply write this:
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 returnnull
.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.