stubbsies12345
stubbsies12345

Reputation: 17

What is wrong with this code Javascript and Parse

I am using javascript and the parse API and i have this code

<script type="text/javascript">
Parse.initialize("APPID", "JAVASCRIPTCODE");
var currentUser = Parse.User.current().get("FirstName");
document.getElementById("fname").value = currentUser;
</script>

<input type=text id="fname" />

however the code is not adding the data from javascript to the text box where am i going wrong.

Upvotes: 0

Views: 26

Answers (1)

Tushar
Tushar

Reputation: 87203

The problem is that you're accessing the element before it is added into DOM.

You can wrap the last statement in DOMContentLoaded

The DOMContentLoaded event is fired when the document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading (the load event can be used to detect a fully-loaded page).

document.addEventListener("DOMContentLoaded", function(event) {
    document.getElementById("fname").value = fname;
});

Or, you can also move your <script> at the end of <body> tag.

Upvotes: 1

Related Questions