I having problem with append() when the textbox is focus the second time.
var i = 1;
$('textarea').live('focusin', function(){
$(this).keydown(function(e){
var code = e.which;
if(code === 13) {
i++;
$('#linenumbers').append('<li>' + i + '</li&开发者_StackOverflow中文版gt;');}
});
This code will append on
third focus it add 3 numbers, and so on..
How can i check that i just add one li each time you hit enter.
Hope you get the problem!
Edit, the html:
<ul id="linenumbers">
</ul>
<textarea></textarea>
when i hit the enter but i get a new line in textarea then it should be two li in linenumbers.
The main problem is that you add another keydown
event handler each time focusin
fires. Try this:
$('textarea').live("keydown", function(e){
var code = e.which;
if(code === 13) {
var prevIndex = parseInt($('#linenumbers li:last').text());
$('#linenumbers').append('<li>' + prevIndex++ + '</li>');
}
});
精彩评论