Reputation: 175
I have a modal form (see HTML snippet for the modal form - this is just a small part of the overall page HTML) to allow users to submit new links to me. This is a local HTML file that I maintain for my team on a shared drive, and we do not have access to serves to host this on, so when a new link is submitted, I just go in and update the HTML with the new link.
I would like to be able to grab the values from each field of the modal-body populate the body of an email, each on it's own line like this:
I'd also like the Subject line to populate with "New Link Submitted" or something like that.
var emailTO = "[email protected]"
var emailSubj = "New Link Submission"
var newName = document.getElementById("newName").value
var newURL = document.getElementById("newURL").value
var newDescrip = document.getElementById("newDescrip").value
var newCat = document.getElementById("newCat").value
var emailBody = newName % OA % OA newURL % OA % OA newDescrip % OA % OA newCat
function emailLink(){
/* ?????? */
}
<div id="myModal" class="modal" align="center">
<div class="modal-content">
<div class="modal-header">
<span id="close" class="close">×</span>
<h3>Submit New Link</h3>
</div>
<div class="modal-body">
<p>Name: <input type="text" class="newName" id="newName" value="newName" </p>
<p>URL: <input type="text" class="newURL" id="newURL" value="newURL" </p>
<p>Description: <input type="text" class="newDesrcip" id="newDesrcip" value="" </p>
<form>
<p>Category:
<select id="newCat" required>
<option value="A"> A </option>
<option value="B"> B </option>
<option value="C"> C </option>
<option value="D"> D </option>
</select>
</p>
</form>
</div>
<br>
<input type="button" id="submit_link_button" value="Submit" onclick="emailLink()">
<!--
I would like to be able to grab the values from each field of the modal-body populate the body of an email, each on it's own line like this:
Name: [value entered in Name field]
URL: [value entered in URL field]
Description: [value entered in Description field]
Category: [value selected from NewCat dropdown field]
I'd also like the Subject line to populate with "New Link Submitted" or something like that.
-->
</div>
</div>
Upvotes: 0
Views: 519
Reputation: 4783
First of all, you have some mistakes in your HTML
code(the input
tags are not closed properly) that I managed to correct them.
You don't need a form
tag to acheive your goal, you can use it but an a
tag would suffice. So, instead of the <input type="button" id="submit_link_button" value="Submit" onclick="emailLink()">
I changed it to <a href="" id="contact-link">Send</a>
tag which has href
attribute that accepts 'mailto' links, and that attribute will be filled by JavaScript
by the desired informations, and I gave it an id="contact-link"
to reference it by JavaScript
.
You used to use an 'inline event handler' in the
input
withtype="button"
(the one that will be replaced by ana
tag) which is a bad practice. Instead, it's better to use the built-inaddEventListener
method to attach an event handler to whatever element you want. Learn more aboutaddEventListener
method.
Here's a runnable snippet the illustrates all what being said.
// select the 'a' tag that has the mailto link to reference later in the code.
var contactLink = document.getElementById('contact-link');
// add click event listener to that 'a' tag.
contactLink.addEventListener('click', function() {
// get all the information required by getting the inputs and the select tags values.
var newName = document.getElementById("newName").value,
newURL = document.getElementById("newURL").value,
newDescrip = document.getElementById("newDesrcip").value,
newCat = document.getElementById("newCat").value,
/* the subject variable holds the subject of the email, change its value per your requirement. */
subject = 'New Link Submitted',
/* the queryString variable holds the email's subject and body to be used in the href attribute of the 'a' tag. the '\n' character is used to make a new line and it must be encoded, along with other special characters as the space, in a valid URL format, we'll be using the built-in 'encodeURI' method for that task. */
queryString = '?subject=' + subject + '&body=Name: ' + newName + '\nURL: ' + newURL + '\nDescription: ' + newDescrip + '\nCategory: ' + newCat,
/* the emailAddr variable holds the email which you want the email to be sent to. Change its value per your requirement. */
emailAddr = '[email protected]';
/* assign the href attribute of the 'a' tag by the queryString variable's value prepended by the desired email adress, and encode all the resulted string in the URL format using the built-in 'encodeURI' method. */
this.href = window.encodeURI('mailto:' + emailAddr + queryString);
// just for testing to see the resulted href string. Delete this line in production phase.
console.log(this.href);
});
<div id="myModal" class="modal" align="center">
<div class="modal-content">
<div class="modal-header">
<span id="close" class="close">×</span>
<h3>Submit New Link</h3>
</div>
<div class="modal-body">
<p>Name: <input type="text" class="newName" id="newName" value="newName" / ></p>
<p>URL: <input type="text" class="newURL" id="newURL" value="newURL" / ></p>
<p>Description: <input type="text" class="newDesrcip" id="newDesrcip" value="" / ></p>
<p>
Category:
<select id="newCat" required>
<option value="A"> A </option>
<option value="B"> B </option>
<option value="C"> C </option>
<option value="D"> D </option>
</select>
</p>
</div>
<br>
<!-- no Form tag is needed, and also the 'input' with 'type=button' is replaced by the next 'a' tag -->
<a href="" id="contact-link">Send</a>
Learn more about the
encodeURI
method.
Ps: in order to make all of this work, the user must configure his default mailing application, otherwise nothing will happen.
Hope I pushed you further.
Upvotes: 1