Get the input type of a textbox
I am making a chrome extension for which I am trying to listen to the mouse clicks using message passing.
I want to know if it is possible to obtain the input type of a textbox when a m开发者_如何学Couse ic clicked on the textbox ?
The type of the input is a property on the element.
For example, you can open chrome inspector on this page and type in the console:
var firstInput = document.getElementsByTagName('input')[0];
firstInput.type; // outputs "text"
edit: you could bind the click on these elements and get the type from the event.target
property of the event.
have your onclick handler take an arg, and pass the object:
<script>
function handler(object) {
alert(object);
}
</script>
<div onclick="handler(this);">this is a test</div>
What exactly are you having trouble with?
If you have the input element you can just do:
theinputelement.getAttribute(type);
Update:
Okay, here's how you would get click events and find out the type of the input element with jQuery:
$(document).click(function(event) {
if (event.which !== 1) {
return;
}
if (event.target.nodeName === 'INPUT') {
alert(event.target.type);
}
});
精彩评论