Search code examples
javascripthtmldynamicmailto

Is it possible to dynamically set the recipient of MAILTO: with only HTML and JavaScript?


I am creating a form using only HTML and JavaScript where I want to be able to dynamically set the recipient of a POSTed form by altering the MAILTO: address or the CC address of the mailto: line. This will be determined by the value of a text box if at all possible. The purpose of this functionality is that the form data needs to be sent to a different manager depending on the choice they make in a drop down menu. I have seen this done in one place (http://javascript.internet.com/forms/multiple-mailer.html) but my issue is that I am populating my dropdown from a JS array which when selected populates some text fields just like (http://www.webdeveloper.com/forum/showpost.php?p=984036&postcount=8). So what I want to do is populate a hidden text input with my drop down and draw from that to determine who gets the email.

I should note that this is for a corporate INTRAnet site where lack of desktop email client is not an issue and where IE is the only browser usable. I do not have access to server-side tech so mailto is my only option, please refrain from wasting comments telling me how bad an idea it is, lol. Thanks!

Thoughts?


Solution

  • To open the mail client with some email address:

    location = 'mailto:' + email_address;
    

    To set the href of an a element, first get it using something like document.getElementById, then set the href property:

    document.getElementById('someLink').href = 'mailto:' + email_address;
    

    email_address is a string containing the applicable email address -- you can replace this with an expression that gets the value of the dropdown:

    document.getElementById('someLink').href = 'mailto:' + document.getElementById('dropdown').value;