javascript - Applying onChange event on a div for all input fields - Stack Overflow

Hello actually I have an div tag in which there are multiple form. What I want is to apply onChange eve

Hello actually I have an div tag in which there are multiple form. What I want is to apply onChange event on the div for each input field. Present I am trying this

Applying form class to each form and getting onchange event

$('.form :input').change(function(e){
   console.log("Element changed");
});

But I am not able to get the info which field is changed and what is the updated value.

So can I apply it on the div itself? If not how can the get the field info which is changed.

Hello actually I have an div tag in which there are multiple form. What I want is to apply onChange event on the div for each input field. Present I am trying this

Applying form class to each form and getting onchange event

$('.form :input').change(function(e){
   console.log("Element changed");
});

But I am not able to get the info which field is changed and what is the updated value.

So can I apply it on the div itself? If not how can the get the field info which is changed.

Share Improve this question edited Feb 3, 2014 at 11:56 harpax 6,1165 gold badges37 silver badges49 bronze badges asked Feb 3, 2014 at 11:52 Nikhil AgrawalNikhil Agrawal 26.6k21 gold badges93 silver badges127 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Try this:

$(function(){
   $('.form :input').change(function(e){
      console.log($(e.target).attr('id'));
   });
});

here e.target is your current element which got the event of change.


As per your ment:

How we can say which field is changed.

you need to assign an unique identifier to all your input type text:

<input type='text' id='a'>
<input type='text' id='b'>
   ..................

then use this:

$('.form :input').change(function(e){
   $('#log').prepend('<p>Form changed ' + $(e.target).attr('id') + '</p>')
});

A demo of your fiddle here.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745102205a4611341.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信