2

I would like to display my "users email" in plain text within my HTML file.

Right now, my project is constructed of three different files:

index.html - edit.html - my_parse_app.js

In my_parse_app.js I declare all of my javascript / jquery functions to help interact with PARSE.com and my backend. There, I am grabbing the users account information (most importantly, the email).

I do this by using:

var email = currentUser.get("email");

I would like to display the variable "email" in my HTML file. Most important h1 if thats possible, just for practice. Would anyone know how to do this easily using two seperate files? I have tried multiple solutions and none of them seem to work.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

3

In your HTML file, give the h1 element that will display the email address a unique id. Then, you can set the innerHTML property of that element to the value of the email variable.

HTML:

<h1 id="userEmail">Placeholder</h1>

JS:

var email = currentUser.get("email");
document.getElementById("userEmail").innerHTML = email;

You'll need to make sure that the DOM is ready before you try to get the element by its id though.

Here's a really simple example: http://jsfiddle.net/jk8yC/

fletch
  • 1,631
  • 1
  • 11
  • 12
  • 3
    Also, [be careful](http://stackoverflow.com/a/70222/1002152) when inserting any user generated content into the page. – marlenunez Jun 06 '13 at 02:43
  • @fletch That doesn't seem to work still. Do you think I might be doing something else wrong? The variable email is showing up when I alert() it just not when I try to put it in h1. –  Jun 06 '13 at 02:53
  • Can you create a jsfiddle demonstrating the failure? My guess is that the DOM element is undefined when you are trying to set the value... – fletch Jun 06 '13 at 03:04
  • @fletch I am unsure about how to do that truthfully. I'm new to web devleopment (usually just do iOS but need this for my project). I can grab "email" within the Javascript file but when switching to HTML it just seems "un-retreivable". –  Jun 06 '13 at 03:14
  • Let me see if I can still help. Where are you loading your JS files? In the `head` or the `body`? – fletch Jun 06 '13 at 03:19
  • @fletch in the head I am loading the files. In the body I am putting the HTML h1 code. The function is being loaded using window.onload in the body. –  Jun 06 '13 at 03:21
  • @fletch I'm not sure what you mean. In my head I import the my_parse_app.js files as well as any other frameworks, etc. Then I go to and load the window.onload script to run the function that grabs the users email. Then, I have a form that is the users profile information. Then I have the h1 code. I'm hoping to make an edit profile page and have the information automatically put into the form fields. –  Jun 06 '13 at 03:25