javascript - Set multiple form values using jQuery Data attribute -


  1. so let have form id #form has 2 input fields, namely title & price.
  2. 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.
  3. the obvious solution works $("#name").val($(this).data('name')); $("#price").val($(this).data('price'));
  4. this looks bad when have many fields. trying work $('#form').data($(this).data());, more or less in single like
  5. 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

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -