Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 ✔✌.