/    Sign up×
Community /Pin to ProfileBookmark

Add variable text to innerHTML

Hi,

This is probably a common question, but I do not know how to ask. It is about fetching data from an api and show on a web page. The results seems to be OK in an alert message, but not presented as a list in the innerHTML.

Any clue what I am doing wrong?

TIA,

[code]
let Text = document.getElementById(“text”)
let URL = “http://127.0.0.1:8081/co”;

function getPost() {
fetch(URL)
.then(res => res.json())
.then((data) => {
data.forEach(function(co) {list += `<li>${co.company}</li>`})
alert(list) <— the list is correct in this variable
Text.innerHTML=(list) <— THE PROBLEM – will NOT show up in the page
Text.innerHTML=(“list”)<— This will show up
})
}[/code]

Edited by Sempervivum: Inserted code tags

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumApr 28.2019 — Please use code tags when posting code, it will increase readability:

https://www.bbcode.org/examples/?id=15

Did you test the code exactly as you posted it? Then the reason is that in your second line
Text.innerHTML=("list") any HTML inserted by the previous line
Text.innerHTML=(list)
is overwritten.
Copy linkTweet thisAlerts:
@SempervivumApr 28.2019 — PS: I tested your code and I needed to initialize the string list by an empty string:
function getPost() {
fetch(URL)
.then(res =&gt; res.json())
.then((data) =&gt; {
let list = ''; // needed to insert this line
data.forEach(function (co) { list += <span><code>&amp;lt;li&amp;gt;${co.company}&amp;lt;/li&amp;gt;</code></span> })
console.log(list);
Text.innerHTML = (list);
//Text.innerHTML = ("list")
})
}
Copy linkTweet thisAlerts:
@KxmodeMay 02.2019 — You may want to look in jQuery's Ajax method. http://api.jquery.com/jquery.ajax/

In general terms Ajax through jQuery looks like this:

<i>
</i>$.ajax({
url: "test.html",
context: document.body
}).done(function() {
$( this ).addClass( "done" );
});


You can then put that inside an event selector.

<i>
</i>$(document).on("click", "#SubmitButton", function() {
$.ajax({
url: "test.html",
context: document.body
}).done(function() {
$( this ).addClass( "done" );
});
});


You can even get fancy by adding a spinner animation while the user waits for the Ajax to finish.

<i>
</i>$(document).on("click", "#SubmitButton", function() {
$(".spinner-image").show();
$.ajax({
url: "test.html",
context: document.body
}).done(function() {
$(".spinner-image").hide();
$( this ).addClass( "done" );
});
});


jQuery frees you up from having to develop the JavaScript fundamentals so you can just do the work. 😀
×

Success!

Help @sibert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...