My question is very similar to this one:
Getting Recipient Names/email address in Notification
However I need to use a select field rather than checkboxes.
I've tried this but it doesn't work (I used .change
instead of .click
and :selected
instead of :checked
):
$(function($){
var $chosenRecipients = $('[name="freeform_product"]');
var $recipientChecks = $('[name^="the_product"]');
$recipientChecks.change(function(){
setTimeout(function(){
$chosenRecipients.val('');
var newVal = [];
$('input[name^="the_product"] option').each(function(){
if ($(this).is(':selected'))
{
newVal.push($(this).val());
}
});
$chosenRecipients.val(newVal.join(', '));
},0);
});
});
The relevant HTML/EE code:
{exp:freeform:form
collection="Contact Us"
form:id="contact_us"
recipients="yes"
recipient_template="contact_us_page"
recipient1="name|[email protected]"
recipient2="name|[email protected]"
recipient3="name|[email protected]"
recipient4="name|[email protected]"
recipient5="name|[email protected]"
}
<input type="text" value="" id="freeform_product" name="freeform_product">
<label for="the_product" class="span6">Product<br>
<select name="the_product" id="the_product">
<option value="">------Select------</option>
{freeform:recipients}
<option value="{freeform:recipient_name}">{freeform:recipient_name}</option>
{/freeform:recipients}
</select>
</label>
Any ideas on what I'm missing?
Thanks!
UPDATE: I've added a fiddle here: http://jsfiddle.net/yL432nvc/