javascript - Set multiple form values using jQuery Data attribute -
- so let have form id #form has 2 input fields, namely title & price.
- i click on edit button somewhere in application has data attributes (e.g data-title="apple" data-price="10")that assigned #form upon clicking button.
- the obvious solution works
$("#name").val($(this).data('name')); $("#price").val($(this).data('price'));
- this looks bad when have many fields. trying work
$('#form').data($(this).data());
, more or less in single like - have tried many ways no success
any appreciated
you create jquery plugin can call element contains data points , have apply data based on key elements within form of same name. example below
$.fn.applydata = function(form) { $form = $(form); $.each($(this).data(), function(i, key) { $form.find('#' + i).val(key); }); };
jsfiddle: http://jsfiddle.net/lcm8s/43/
Comments
Post a Comment