javascript : built an html form from json, but can't update the form

masCh Kisiel

I have a javascript code that takes objects from json.

from this, i built an html string:

   var htmlstr = "<table border=0>";
    for (var i=0;i<jsonData.people.length;i++) {
        htmlstr=htmlstr+"<tr><td>" + jsonData.people[i].name + "</td>";
        htmlstr=htmlstr+"<td>"+ jsonData.people[i].cash + "</td>";
        htmlstr=htmlstr+"<td><button onclick='changeCash(i)'>Update</button></td></tr>";
        }
    htmlstr=htmlstr+"</table>";

layer.addFeatures(features);
layer.events.on({ "featureselected": function(e) { updateMak('mak', htmlstr) } });

    function changeCash(k) {
       jsonData.people[k].cash=jsonData.people[k].cash+100;
       }

The HTML page is as follows:

    <script type="text/javascript">
        if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
                  alert("Mobile device detected.");             }
                function updateMak(id,content) {
                    var container = document.getElementById(id);
                    container.innerHTML = content;
                } </script> 
<div id="mak"> List of People  </div>

Lets say this displays 10 people with their money. If I click on one of their Update buttons, I think the json data is updated as intended. But I don't know how to verify it. The values in the form doesn't update the new value from the changeCash function.

How do I update the htmlstr and also update what's already displayed on screen?

Please advise.

user3192244

When you generate htmlstr for the people cash

htmlstr=htmlstr+"<td>"+ jsonData.people[i].cash + "</td>";

You should also generate id for this td so that you can update the content from the function changeCash(k).

Something like

htmlstr=htmlstr+"<td id='peoplecash"+i+"'>" + jsonData.people[i].cash + "</td>";

And then in your changeCash function

function changeCash(k) {
   jsonData.people[k].cash=jsonData.people[k].cash+100;
   var peoplecash= document.getElementById("peoplecash"+k);
   peoplecash.innerHTML = jsonData.people[k].cash;}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update HTML form with variable from previous form

From Dev

Can't Update ObjectListView On Main Form From Class

From Dev

Can't update database entry from form fields

From Dev

Can't get JavaScript method to validate my HTML form

From Dev

I can't update my rails form

From Dev

Can't update value on the main form

From Dev

Dynamic javascript jSon built form not submitting through ajax - Phonegap

From Dev

Reset a form in AngularJS from HTML or in-built directive

From Dev

Reset a form in AngularJS from HTML or in-built directive

From Dev

can't keep data of textarea in an html form

From Dev

Django can't process html form data

From Dev

JavaScript - HTML form to JSON (empty values) and format

From Dev

JavaScript - HTML form to JSON (empty values) and format

From Dev

auto update form with javascript

From Dev

Getting from html form to javascript variable

From Dev

JavaScript not taking values from HTML form

From Dev

Passing javascript valiables from HTML form to PayPal

From Dev

Output HTML Form Element From Javascript

From Dev

getting dynamic values from html form to javascript

From Dev

Getting data from the HTML form into JavaScript

From Dev

Can't update a mongo value with a dynamic array id from a meteor form

From Dev

Can't read form data from HttpServletRequest

From Dev

Can't get option value from form

From Dev

HTML Form using php doesn't update mysql

From Dev

how can i use an input from html form in my javascript function?

From Dev

Codeigniter Jquery Ajax, Can't access data from controller back to html form

From Dev

How do I use Javascript, not jQuery, to generate a JSON list from HTML form data?

From Dev

How do I input text via an HTML form and retrieve data from a JSON file as a response using Javascript?

From Dev

Can't update to rails 4.1: Conflict with simple_form

Related Related

  1. 1

    Update HTML form with variable from previous form

  2. 2

    Can't Update ObjectListView On Main Form From Class

  3. 3

    Can't update database entry from form fields

  4. 4

    Can't get JavaScript method to validate my HTML form

  5. 5

    I can't update my rails form

  6. 6

    Can't update value on the main form

  7. 7

    Dynamic javascript jSon built form not submitting through ajax - Phonegap

  8. 8

    Reset a form in AngularJS from HTML or in-built directive

  9. 9

    Reset a form in AngularJS from HTML or in-built directive

  10. 10

    can't keep data of textarea in an html form

  11. 11

    Django can't process html form data

  12. 12

    JavaScript - HTML form to JSON (empty values) and format

  13. 13

    JavaScript - HTML form to JSON (empty values) and format

  14. 14

    auto update form with javascript

  15. 15

    Getting from html form to javascript variable

  16. 16

    JavaScript not taking values from HTML form

  17. 17

    Passing javascript valiables from HTML form to PayPal

  18. 18

    Output HTML Form Element From Javascript

  19. 19

    getting dynamic values from html form to javascript

  20. 20

    Getting data from the HTML form into JavaScript

  21. 21

    Can't update a mongo value with a dynamic array id from a meteor form

  22. 22

    Can't read form data from HttpServletRequest

  23. 23

    Can't get option value from form

  24. 24

    HTML Form using php doesn't update mysql

  25. 25

    how can i use an input from html form in my javascript function?

  26. 26

    Codeigniter Jquery Ajax, Can't access data from controller back to html form

  27. 27

    How do I use Javascript, not jQuery, to generate a JSON list from HTML form data?

  28. 28

    How do I input text via an HTML form and retrieve data from a JSON file as a response using Javascript?

  29. 29

    Can't update to rails 4.1: Conflict with simple_form

HotTag

Archive