1

I have this function within Javascript;

function newripple() {
  var api = new ripple.RippleAPI();
  var account = api.generateAddress();
  document.getElementById("address").value = account.address;
  document.getElementById("secret").value = account.secret;
}

What I would like to do is instead of assigning these values to input fields I would like to display them on the page as JSON.

My index page is very simple;

<h2>Ripple Wallet Generator</h2>
<p><b>Ripple Address</b></p>
<input readonly="readonly" id="address" style="width: 300px;">
<p><b>Ripple Secret</b></p>
<input readonly="readonly" id="secret" style="width: 300px;">

I am not very familiar with JavaScript I very much appreciate any help.

The desired result when i load the page would be something like ;

{"address":"VALUE","secret":"VALUE"}
kometen
  • 6,536
  • 6
  • 41
  • 51
Lewis
  • 170
  • 7
  • What is the expected result? And what have you tried so far to solve this on your own? - [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – Andreas Jul 27 '21 at 13:03
  • the desired result would be for the account and secret to display like json ecoded with the two values on the page instead of the values being put into the input fields; so like when i lost the page its something like {"account":"VALUE","secret":"VALUE"} – Lewis Jul 27 '21 at 13:04
  • Take a look at this question: https://stackoverflow.com/questions/883977/display-json-as-html – Torsten Barthel Jul 27 '21 at 13:05
  • What is `account`? Is it JSON, is it an object? – Andy Jul 27 '21 at 13:11
  • @Andy its an object – Lewis Jul 27 '21 at 13:17

3 Answers3

2

Convert the object to a string, and add the result to the page.

const account = { address: '10 Albert Street', secret: 'secret' };

const div = document.querySelector('div');

div.textContent = JSON.stringify(account);
<div />
Andy
  • 61,948
  • 13
  • 68
  • 95
0

You can try using the JSON.stringify

const json = '{"address":1, "secret":2}';
const obj = JSON.parse(json);
var i = JSON.stringify(obj)
console.log(i);
oguzhancerit
  • 1,436
  • 1
  • 16
  • 27
-1

Create a JSON object like this -

let json = {'address':account.address, 'secret':account.secret};
document.getElementsByTagName('body')[0].innerHTML = JSON.stringify(json)

Now its upto you how you want to show this

Amit Gupta
  • 252
  • 3
  • 8
  • i tried `document.getElementsByTagName('body')[0].innerHTML = json; ` to write it to the body but all i get is `[object Object]` – Lewis Jul 27 '21 at 13:24
  • Try document.getElementsByTagName('body')[0].innerHTML = JSON.stringify(json) – Amit Gupta Jul 27 '21 at 13:33
  • You should edit your answer because at the moment it doesn't make any sense. That's not JSON. – Andy Jul 27 '21 at 13:41