Lewis
Lewis

Reputation: 300

Display var as json on page

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"}

Upvotes: 1

Views: 85

Answers (3)

Amit Gupta
Amit Gupta

Reputation: 252

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

Upvotes: -1

Andr&#233; Ragazzi
Andr&#233; Ragazzi

Reputation: 1

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);

Upvotes: 0

Andy
Andy

Reputation: 63524

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 />

Upvotes: 2

Related Questions