admin管理员组文章数量:1277543
I have a text box in my page:
<td>
<input type="text" id="Approvers1" size="11" onkeydown="AddUsersKeyDown(event)" style="width: 470px;" />
</td>
This is my js function:
function AddUsersKeyDown(evtobj) {
if (evtobj.keyCode == 75 && evtobj.ctrlKey) {
AddUsers(--input parameter--);
}
}
Now if i press ctrl k in my textbox it must call another javascript function named AddUsers(--input parameter--).. where the input parameter should be the id of the textbox...
How to achieve this.. Either i need the id of the textbox or atleast the DOM object so that i can access the id of textbox from DOM object..
Kindly help me in achieving either one of the two things..
I have a text box in my page:
<td>
<input type="text" id="Approvers1" size="11" onkeydown="AddUsersKeyDown(event)" style="width: 470px;" />
</td>
This is my js function:
function AddUsersKeyDown(evtobj) {
if (evtobj.keyCode == 75 && evtobj.ctrlKey) {
AddUsers(--input parameter--);
}
}
Now if i press ctrl k in my textbox it must call another javascript function named AddUsers(--input parameter--).. where the input parameter should be the id of the textbox...
How to achieve this.. Either i need the id of the textbox or atleast the DOM object so that i can access the id of textbox from DOM object..
Kindly help me in achieving either one of the two things..
Share Improve this question edited Apr 25, 2013 at 13:39 Xavier asked Apr 25, 2013 at 12:58 XavierXavier 1,7946 gold badges27 silver badges46 bronze badges4 Answers
Reset to default 4First you should rename the function to AddUsersKeyDown
so the keydown event will be triggered. On the event object, there is a target
property, which have the triggering DOM element. You can use that information to get the id.
<input type="text" id="Approvers1" size="11" onkeydown="AddUsersKeyDown(event)" style="width: 470px;" />
And the Javascript:
function AddUsersKeyDown(evtobj) {
var target = evtobj.target || evtobj.srcElement;
if (evtobj.keyCode == 75 && evtobj.ctrlKey) {
AddUsers(target.id);
return false;
}
}
Here is a jsfiddle: http://jsfiddle/q26Nv/3/
Use this
to get the DOM element or this.id
to get the id of the element
<td>
<input type="text" id="Approvers1" size="11" onkeydown="AddUsersKeyDown(event,this.id)" style="width: 470px;" />
</td>
function AddUsersKeyDown(evtobj,id) {
if (evtobj.keyCode == 75 && evtobj.ctrlKey) {
AddUsers(id);
}
}
get jquery here
and then take of onkeydown
<input type="text" id="Approvers1" class="keydown" size="11" style="width: 470px;" />
add script
<script src="//ajax.googleapis./ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
$(function() {
$('.keydown').keydown(function() {
console.log( $(this).attr('id') );
});
});
<script>
maybe a lot of code, but you can be sure it's cross browsers documentation
You can:
function AddUserKeyDown(evtobj) {
evtobj = evtobj || window.event;
var target = evtobj.target || evtobj.srcElement;
if (evtobj.keyCode == 75 && evtobj.ctrlKey) {
AddUsers(target.id);
}
}
(Your func names don't match btw)
本文标签: javascriptHow to get the id of the textbox on keydown event of that textboxStack Overflow
版权声明:本文标题:javascript - How to get the id of the textbox on keydown event of that textbox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741201756a2357364.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论