Well, I'm curious why you need to update a hidden field and have your data duplicated. But if you insist, here is a "one size fits all solution", although you may lose some flexibility doing this instead of manually attaching each element.
First, the HTML:
<form id="form"> <input type="text" id="txtName" /> <input type="hidden" id="hdnName" /> <input type="text" id="txtEmail" /> <input type="hidden" id="hdnEmail" /> <select id="selGender"> <option value="male">Male</option> <option value="female">Female</option> </select> <input type="hidden" id="hdnGender" /> </form>
Now, the Javascript:
$(document).ready(function() { $("#form").find("input[type='text'], select").change(function() { $this = $(this); var id = $this.attr("id"); id = id.split(3, id.length - 1); $("#hdn" + id).val($this.val()) }); });
For this to work, it assumes you have each of your fields as text fields, also that they follow the naming convention I used here ([txt|sel]FieldName, hdnFieldName)