1

I wanna set HTML form elements value using json object by jquery. I have done adding the values by serializeArray() in an HTML localstorage.

var formData = $('#page-2').serializeArray();
formData.forEach(function(field){
    var dataDInfo = {};
    if($.inArray(field.name, propertyDList) >= 0){
        if($('#'+field.name).data('displayName')){
            dataDInfo['present'] = field.value;
            dataDInfo[displayName] = $('#'+field.name).data('displayName');
            list.propertyDCategory[field.name] = dataDInfo;
        } else {`enter code here`
            list.propertyDCategory[field.name] = field.value;
        }
    }
});

now i am having all values in json object say list. I am getting all data by using

var lists = JSON.parse(localStorage.getItem('list'));

eg: json
{
  aa:ab,
  bb:{
    cc:cb,
    dd:db
  },
  ee:eb
}

Now i want to write a edit and update function and fill all the elements value by using json data based on json data field (name of HTML element is equal to json data field) after that i wanna update the local storage list.

Note : html form contain input, radio button, check list, select types of elements.

elements are changing based on select element.

Can anyone help me to write the code?

Thanks.

1 Answer 1

2

Iterate over each json object in your list and then use the key to select your input element using the jquery selector. Use the val method to set the value of the input element.

It will be something like this:

for (var key in lists) {   
  $('[name="'+key+'"]').val(lists[key]);
}

jsFiddle example

Sign up to request clarification or add additional context in comments.

4 Comments

please provide an example.
I provided you with a jsFiddle example
It is not working for nested JSON data as i have asked in question.
I don't understand what you want to do with nested json data. Please give an example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.