Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keyup events using class instead of id

If I have 2 textboxes, without using id's, how do I determine which textbox is firing off "keyup" events?

For various reasons I need to use classes instead of id's, where the class name is the same for both textboxes.

Infact, I could have the same textbox on the screen many times with the same class name.

The HTML looks something like this

<div class="outer_div">
   <input type="text" class="mytextbox" />
   <div class="inner_div"></div>
</div>

<div class="outer_div">
   <input type="text" class="mytextbox" />
   <div class="inner_div"></div>
</div>

<div class="outer_div">
   <input type="text" class="mytextbox" />
   <div class="inner_div"></div>
</div>
like image 266
oshirowanen Avatar asked Dec 13 '22 02:12

oshirowanen


2 Answers

$('.mytextbox').keyup(function(event){
 // play with event
 // use $(this) to determine which element triggers this event
});
like image 144
Vishal Avatar answered Dec 24 '22 04:12

Vishal


You can use the data- attribute to assign an id eg..

<input data-id="x" />

Then do something like.

console.log($(this).data('id'));
like image 39
Steve Avatar answered Dec 24 '22 04:12

Steve