Click here to Skip to main content
15,891,895 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
$(function () {
          document.getElementById("txtSearch").value = "hai";
      });

C#
<input class="width150" type="text" id="txtsearch"   />

i have a text box . I want to assign value to textbox in jquery. But its showing an error message like this
Uncaught TypeError: Cannot set property 'value' of null

Can anyone help me????
Posted
Updated 12-Apr-17 21:12pm
v3

You need a lower-case value and a plural Elements:
for example:
JavaScript
document.getElementsByName('ContactEmail')[0].value = email;


You need the [0] to get the first element in the list. Names don't have to be
unique like ids.
 
Share this answer
 
Comments
Aswathi Narayan 21-Sep-13 1:14am    
Thanks alot :)
[no name] 21-Sep-13 1:16am    
ur most welcome..please if the answer is helpful mark it as accept solution for future reference...:)
It simply means that the element was not found. If its id value is "txtSearch" (check it up, should be id="txtSearch", and see how it looks in raw HTML on the client side, using "View Page Source"), the jQuery selector must be written as:
JavaScript
myElement = $("#txtSearch");
myElement.value = "hai";


One possible bug could be wrong id due to wrong use of ASP.NET ClientID. Check it up:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx[^],
http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx[^].

—SA
 
Share this answer
 
Check this out hope it may help you,

JavaScript
<script type="text/javascript">
        function OnButtonClick() {
            var textcontrol = document.getElementById("txtname");
            textcontrol.value = "Arun";
        }

    </script>


<pre lang="HTML">

 <input type="text" id="txtname" />
    <input type="button" id="btnsubmit" onclick="OnButtonClick()" value="Submit" />

 
Share this answer
 
 
Share this answer
 
Comments
Aswathi Narayan 21-Sep-13 1:12am    
But my textbox is input type.. not server side control :(
You can use like this
document.getElementById('txtSearch').value = "Set Value To textbox using javascript";

You can also use Jquery to set textbox value.
$("#txtSearch").val('Set Value To textbox using Jquery');
 
Share this answer
 
Comments
CHill60 13-Apr-17 4:58am    
Post is over 3 years old and already answered!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900