I have a form which has the fields recipient and email. The user has the option to add more recipients.
<div class=" form-inline">
<label for="exampleInputEmail1">Your Recipients</label>
<script>
var counter = 1;
var limit = 50;
function addInput(divName){
if (counter == limit) {
alert("You have reached the limit of adding " + counter + " inputs");
}
else {
var newdiv = document.createElement('div');
newdiv.innerHTML = "<br> <input class='form-control reciptest' placeholder='Recipient Name' type='text'> <input class='form-control emailtest' placeholder='Recipient Email' type='text'>"
document.getElementById(divName).appendChild(newdiv);
counter++;
}
}
</script>
<div id="dynamicInput">
<input class='form-control reciptest' placeholder='Recipient Name' type='text'> <input type="text" class="form-control emailtest" placeholder="Recipient Email">
<br>
</div>
<input type="button" class="btn btn-sm btn-inverse" value="Add Another Email" onClick="addInput('dynamicInput');">
</div>
Inside the email processing function the inputs of the two classes are split into an array like this
var emails = document.getElementsByClassName( 'emailtest' ),
email = [].map.call(emails, function( email ) {
return email.value;
}).join( ',' );
var names = document.getElementsByClassName( 'reciptest' ),
rname = [].map.call(names, function( rname ) {
return rname.value;
}).join( ',' );
Here is where my problem lies
This code currently works to push the emails into the array,but I am unable to add the recipients email into the array
var to =[];
email.split(',').forEach(function(k){ to.push ( {email:k, type:'to'});
rname.split(',').forEach(function(r){ to.push ( {email:k, type:'to', name:'r'});
How do i go about merging these two arrays and keeping them in the order it was entered ?
According to your code, you seem to try to merge both emails and recipient names in to the array to
. So, you may try this:
var to =[];
var emails = email.split(',');
var rnames = rname.split(',');
emails.forEach(function(em,i){
var recipient = {email: em, type: 'to', name: null};
if (rnames.length>i)
recipient.name = rnames[i];
to.push(recipient);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.