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

General Discussion

MULLY BENDET
MULLY BENDET
509 Points

why did it not work

<html>

email:<br> <input type="text" id="email" name="email" value="empty">

<script> function myFunction1() { var input = document.getElementById('email'); email = input.value;

    window.open("mailto:" + email + "?subject=" + subject);
  }

</script> </html>

2 Answers

David Perkins
David Perkins
9,607 Points

Hey man!

Firstly, there wasn't a way for the function to actually be fired so i added a submit trigger. I also added the subject input so that it would fill the declared variable. You also don't need the 'value' element within the input so i swapped that out for a placeholder instead.

This should now work and give you some food for thought if you want to expand it or tweak it to trigger with a different event or integrate it into some other purpose.

I hope this helps though... let me know how you get on ??

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title>Treehouse</title>
    </head>
    <body>
        <form id="emailForm">
            <p>Email:<br> <input type="text" id="email" name="email" placeholder="Email Address" /></p>
            <p>Subject:<br> <input type="text" id="subject" name="subject" placeholder="Subject" /></p>
            <p><button type="submit">Send Email</button>
        </form>
    </body>

    <script>
        // Call the function on submit of the form
        document.getElementById("emailForm").onsubmit = function() { myFunction1() };

        // Declare the function
        function myFunction1() {
            // Get email input value
            var email = document.getElementById('email').value;

            // Get subject input value
            var subject = document.getElementById('subject').value;

            // Create mailto window
        window.open("mailto:" + email + "?subject=" + subject);
      };
    </script>
</html>
Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

Next time you should post this in the html section of the forums though at least you have a great answer ✔✌.