DON
DON

Reputation: 835

How to get id's of textboxes on button click?

<table>
<tr>
<td><input id="txt1" type="text"></td>
<tr>

<tr>
<td><input id="txt2" type="text"></td>
<tr>

<tr>
<td><input id="txt3" type="text"></td>
<tr>

<tr>
<td><input id="txt4" type="text"></td>
<tr>
</table>

When i click on each text box, on a keyup event i should get id's of each textbox, what i should do.

Upvotes: 1

Views: 781

Answers (3)

Sadikhasan
Sadikhasan

Reputation: 18600

$("table input").keyup(function(){
  alert($(this).attr("id"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table>
<tr>
<td><input id="txt1" type="text"></td>
<tr>

<tr>
<td><input id="txt2" type="text"></td>
<tr>

<tr>
<td><input id="txt3" type="text"></td>
<tr>

<tr>
<td><input id="txt4" type="text"></td>
<tr>
</table>

Upvotes: 1

Anubhav Chaudhary
Anubhav Chaudhary

Reputation: 133

$('input').keyup(function(){alert($(this).attr('id'))}

same thing for all textboxes

Upvotes: 1

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

Use like this:

$('input').on('keypress',function(){//you may bind "keyup", "change", etc. event
  console.log($(this).attr('id'));
});

See: attr, on,keypress

Upvotes: 1

Related Questions