Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

is it possible to put a var inside a html document

if possible can you give me an example

6 Answers

Adding a subject isn't to hard. It works a lot like query language. All you do is add a question mark with subject and then define the subject.

So first I will give you an example of what it looks like to add a subject:

<a id="myId" href="mailto:jonathan@test.com?subject=My random subject!">Mail to with subject</a>

You can also, btw, add a body like this:

<a id="myId" href="mailto:jonathan@test.com?subject=My random subject&body=This is some text to start the body!">Example with a body</a>

So combine that and here is what that looks like in the script tag:

var myEmail = "jonathan@test.com";

var subject = "This is my random subject!";

var starterBody = "Just a start to the body";

// version one with the subject added:
document.getElementById("myId").href = "mailto:" + myEmail + "?subject=" + subject;

// version two with the subject and body added:
document.getElementById("myId").href = "mailto:" + myEmail + "?subject=" + subject + "&body=" + starterBody;

Hi mully!

Basically, it's possible although you have to wrap it inside <script>var variable = "variable";</script> tags. Then you can use it in HTML element with getElementById("insert-id-here").

Hope this helps!

i don't get it

Here is an example of what they are talking about. Given the following html:

<h1 id="myId">Hello World!</h1>

<script>
var myGreeting = "Hello Mully!";

document.getElementById("myId").innerHTML = myGreeting;
</script>

The script when run will create the variable, find the element that has the id "myId" and change the inner HTML to the previously created variable's string.

Does that make sense?

it makes sense but that's not what i want

i want to make a var instead of a email in the mailto

<a href="mailto:"var"></a>

Great example Jonathan!

A way to do that is simply change the href value and add it to the mailto.

So for example, if I am getting your question clearly, do the following:

<a id="myId" href="#">Mail!</a>

<script>
var myEmail = "mully@teamtreehouse.com";

document.getElementById("myId").href = "mailto:" + myEmail;
</script>

The only difference between my previous example is instead of dealing with the inner HTML, we call the value of the href and concatenate the "mailto:" string with a string of the email you want to email it to. I hope that helps!

Brilliant Jonathan!

Thanks!

thank you but what if i wanted to add a subject

I made an example in jsfiddle. It probably makes lot more sense than my previous reply :)

https://jsfiddle.net/pruse/3fgdnjtf/

what if i want to put a var in a mail to