Click here to Skip to main content
15,891,908 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello

why this code Not work :
JavaScript
$(document).ready(function () {

        var textbox = document.getElementById("text1");
        //event key down and key up and keypress
        textbox.onkeydown(function () {
            alert();
        });

    });


error:
TypeError: textbox is null


html mvc :
Razor
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<input type="text" id="text1 " />


i want to learn javaascript and jquery what is the best book (2013 or 2014) for them ?
Posted
Updated 2-May-14 5:25am
v3
Comments
CHill60 2-May-14 11:20am    
You haven't got an element called "text1" ... is it "Text1" perhaps?
thatraja 2-May-14 11:21am    
Share the HTML content of page in your question
CHill60 2-May-14 11:30am    
Change id="text1 " to id="text1"
NorouziFar 2-May-14 11:33am    
book?
CHill60 2-May-14 11:40am    
Have a look here A Gallery of Useful Programming Books[^] - did the fix work?

Try this:
var textbox = $('#text1');
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-May-14 18:33pm    
5ed.
I added Solution 2 with some explanations to it.
—SA
If you already using jQuery, using document.getElementById is just pointless; it is already done in more general way, through selectors:
http://api.jquery.com/category/selectors[^].

If you know the id value, you can use the id selector (see also the sample in Solution 1): http://api.jquery.com/id-selector/[^].

—SA
 
Share this answer
 
You need to fix two errors.
1. correct the text id, remove the extra space.
<input type="text" id="text1" />

2. assign a function to onkeydown:
C#
textbox.onkeydown = function () {
            alert("onkeydown");
        };
 
Share this answer
 

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