Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  3] [ 3]  / answers: 1 / hits: 15433  / 11 Years ago, thu, october 10, 2013, 12:00:00

I create the same inputbox a couple of times dynamically. When a user presses enter it calls a function. I then test with this code:



function insertComment(){
alert($(this).attr('id'));
}


But it keeps returning undefined. Here is the syntax I use to create similiar input boxes:



$(#divToAppend).append(<input id='testID' type = 'text' class = 'formatCSS' style = 'margin-top:12px ; ' placeholder = 'Add Comment' onkeydown='if (event.keyCode == 13) insertComment()'></input>);

More From » jquery

 Answers
19

Just pass this while calling method :



$(#divToAppend).append(<input id='testID' type = 'text' class = 'formatCSS' style = 'margin-top:12px ; ' placeholder = 'Add Comment' onkeydown='if (event.keyCode == 13) insertComment(this)'></input>);


function insertComment(this)
{
alert($(this).attr('id'));
}

[#75090] Wednesday, October 9, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
coby

Total Points: 27
Total Questions: 102
Total Answers: 97

Location: Honduras
Member since Wed, Jul 14, 2021
3 Years ago
;