Search code examples
javascriptjqueryformsonkeyup

How do pass the id of a form onKeyUp?


There's plenty of questions on how to get the value onKeyUp, but I want to pass the id of the form onKeyUp as well.. I tried something like this, but it's telling me getId is not defined.

function getId(x){
$('.not').append(x);
}

var word = 'HELP';

$('.why').html("<form class='questions'><input type='text' id='"+word+"' 
name='"+word+"' onkeyup='getId("+word+")'></form> ");

http://jsfiddle.net/evs3A/

Also is putting something like "+variable+" bad practice, because I'm using it quite a lot ;)? Thank u.


Solution

  • Use jQuery to hook up the event and avoid the problem altogether. Try this:

    var word = 'HELP';
    $('.why').html("<form class='questions'><input type='text' id='" + word + "' 
    name='" + word + "'></form> ");
    
    $('.questions input').on('keyup', function(e) {
        $('.not').append(this.id);
    });
    

    Example fiddle