jquery populate form json

Solutions on MaxInterview for jquery populate form json by the best coders in the world

showing results for - "jquery populate form json"
Tommaso
06 Aug 2016
1//Plugin  
2(function($){
3    $.fn.setFormData = function(data){
4        let t = this;
5        $.each(data, function(key, value) {  
6            var ctrl = $(t).find('[name='+key+']');  
7            switch(ctrl.prop("type")) { 
8                case "radio": case "checkbox": case "select":   
9                    ctrl.each(function() {
10                        if($(this).attr('value') == value) $(this).attr("checked",value);
11                    });   
12                    break; 
13                    case "select" :
14                    // manipulate select?
15                    ctrl.val(value); 
16                    break;
17                default:
18                    ctrl.val(value); 
19            }  
20        });  
21    }
22  })(jQuery);
23
24//Call
25$('#form').setFormData(someData);