javascript - getting current element in onKeydown function - Stack Overflow

i am writing a function which will be called when a key is down on keyboard how to access value of that

i am writing a function which will be called when a key is down on keyboard how to access value of that textbox in which key is pressed. my code is

function isValid(evt,that){

console.log('ing to this 1');
var charCode = (evt.which) ? evt.which : event.keyCode;
console.log(that.val());
return true;
}

$(document).on('keydown','.is_valid',isValid);

how to get the value of textbox which is currently getting input from keyboard ?? please guideline How to acplish this

i am writing a function which will be called when a key is down on keyboard how to access value of that textbox in which key is pressed. my code is

function isValid(evt,that){

console.log('ing to this 1');
var charCode = (evt.which) ? evt.which : event.keyCode;
console.log(that.val());
return true;
}

$(document).on('keydown','.is_valid',isValid);

how to get the value of textbox which is currently getting input from keyboard ?? please guideline How to acplish this

Share Improve this question asked Jul 12, 2013 at 9:00 mathlearnermathlearner 7,65932 gold badges131 silver badges191 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

I'd suggest:

function isValid(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode,
        self = evt.target;
    console.log(self.value);
    return true;
}

$(document).on('keydown', '.is_valid', isValid);

JS Fiddle demo.

So long as the event is available to the function, you can access the target of that event with the appropriately-named event.target. If you need that to be a jQuery object, with access to jQuery methods you can use: $(event.target).

You could also use:

self = evt.currentTarget;

JS Fiddle demo.

Or:

self = document.activeElement;

JS Fiddle demo.

References:

  • document.activeElement.
  • event.currentTarget.
  • event.target.

this inside the event handler will point to the targeted element

function isValid(evt,that){
    var charCode = (evt.which) ? evt.which : event.keyCode;
    console.log('log', $(this).val(), charCode);
    return true;
}

$(document).on('keydown','.is_valid',isValid);

Demo: Fiddle

you can get the current element using $(this)

$(document).on('keydown','.is_valid',isValid){
alert($(this).val());

 //and  do stuff here 

}

change that.val() to $(this).val()

function isValid(evt) {
    console.log('ing to this 1');
    var charCode = (evt.which) ? evt.which : event.keyCode;
    console.log($(this).val());   // change that to $(this)
    return true;
}

$(document).on('keydown', '.is_valid', isValid);

Working good. Check JSFiddle

There are a few different ways to get this. Below are three examples:

function isValid(evt) {
    // if you use 'this', you don't need to pass 'evt' to the function
    console.log(this.value);

    // you can reference the event target
    console.log(evt.target.value);

    // you can reference the event's current target
    console.log(evt.currentTarget.value);

    return true;
}

$(document).on('keydown', '.is_valid', isValid);

These will all give you the value of the textbox.

If you want to see what other options might be available, console.log(evt);.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742301377a4418094.html

相关推荐

  • javascript - getting current element in onKeydown function - Stack Overflow

    i am writing a function which will be called when a key is down on keyboard how to access value of that

    22小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信