Thursday, 15 April 2010

javascript - How do I update data embedded in the HTML script tag? -


I have a simple HTML page with Jason-LD data embedded in the script tag.

How do I update this data when submitting this form?

This page has some code (data + load function). What I'm missing is a savings data function that updates the data embedded in the script section.

Thanks,

  & lt; Script id = "person" type = "application / ld + json" & gt; {"@contact": "http://schema.org", "@type": "person", "given name": "marco", "family name": "van basten"}    

The following code snippet is for example only , but You should show how to update your script object with JSON strings by using JSON.stringify .

person.innerHTML = JSON.stringify (jsonld);

Change the original load value using Load then Save and Reload Use the Browser Developer Tool to see the values ​​of the DOM.

  function load data (e) {var person = document.getElementById (" person "); Var jsonld = JSON.parse (person.textContent); Console.log (jsonld.givenname, jsonld); Document.getElementById ("givenName"). Value = jsonld.givenName; Document.getElementById ("familyName"). Value = jsonld.familyName; } Function saveData (e) {console.log ('event', e); Var person = document.getElementById ("person"); Var jsonld = JSON.parse (person.textContent); Jsonld.givenName = document.getElementById ("givenName"). value; Jsonld.familyName = Document.getElementById ("Family Name"). Values; //person.innerHTML = JSON.stringify (jsonld); Person.textContent = JSON.stringify (jsonld); } Document.getElementById ("saveButton"). AddEventListener ('click', saveData); AddEventListener ('click', load data);  
  & lt; Script id = "person" type = "application / ld + jason" & gt; {"@contact": "http://schema.org", "@type": "person", "given name": "marco", "family name": "van basten"}  & Lt; Input type = "article" id = "family name" /> & lt; / Form & gt; & Lt; Button ID = "Load Button" & gt; Load & lt; / Button & gt; & Lt; Button id = "savebutton" & gt; Save & lt; / Button & gt;  


No comments:

Post a Comment