Reputation: 1106
I am dynamically generating textboxes in ASP page and suppose they are calling javascript function "foo" on text change. I want to pass the textbox ID calling the function to that javascript function.
i.e. If you are typing in textbox 3, I wanna show that Currently You are typing in txtbox3 (the id of textbox 3)
I have an autocomplete function I dont know how to pass the ID of the calling textbox.. I am using class attribute but it changes the value of every textbox... I want the ID instead of .tbi
Upvotes: 0
Views: 1204
Reputation: 707326
There are a couple answers depend upon how your code works.
If you are attaching the event handler with addEventListener, then you can get the object that generated the event from the this
pointer like this:
obj.addEventListener("change", function() {
// get the id of the object that triggered the event
var id = this.id;
// do whatever you want to with that id
}, false)
or using a regular function:
function foo(e) {
// get the id of the object that triggered the event
var id = this.id;
// do whatever you want to with that id
}
obj.addEventListener("change", foo, false);
or, if you are embedding the event handler directly in the HTML, you can do it like this:
<textarea id="xxx" onchange="foo(this)"></textarea>
function foo(obj) {
// get the id of the object that triggered the event
var id = obj.id;
// do whatever you want to with that id
}
As is usually the case on stackoverflow, if you show us your actual HTML and javascript, you will get answers specific to your particular situation.
Upvotes: 1
Reputation: 318508
That highly depends on how you call foo
.
Let's assume you use inline onkeydown="foo();"
on all textboxes. Then you simply change it to onkeydown="foo.call(this);"
. Then you can use this
inside foo and access the ID via this.id
.
Upvotes: 0