Answers for "jquery populate form json"

1

jquery populate form json

//Plugin  
(function($){
    $.fn.setFormData = function(data){
        let t = this;
        $.each(data, function(key, value) {  
            var ctrl = $(t).find('[name='+key+']');  
            switch(ctrl.prop("type")) { 
                case "radio": case "checkbox": case "select":   
                    ctrl.each(function() {
                        if($(this).attr('value') == value) $(this).attr("checked",value);
                    });   
                    break; 
                    case "select" :
                    // manipulate select?
                    ctrl.val(value); 
                    break;
                default:
                    ctrl.val(value); 
            }  
        });  
    }
  })(jQuery);

//Call
$('#form').setFormData(someData);
Posted by: Guest on June-30-2020

Code answers related to "jquery populate form json"

Code answers related to "Javascript"

Browse Popular Code Answers by Language