So what I’m trying to do right now is give an email value to the selected option on button click to an email form that I’ve made using contact form 7, right now I have the options appended to the select element from a variable that has the department name and email stored like this {Example: “example@email.com”, Example2: “example2@email.com”, etc}. So at the moment it just appends the department name like this

<select>
 <option>Example</option>
 <option>Example2</option>
 <option>etc</option>
</select>

So I what I want it to do once the user clicks the submit button is have it append or give the corresponding option its email value, any help on this would be great even if it’s just pointing me to the right direction.

Here is my other code that grabs and stores the information

This grabs and stores the department names from a custom post type

 var department = $('.contact-us-department').map(function() {
    return this.innerHTML;
    }).toArray();

This grabs and stores the emails from a custom post field in the custom post type

 var email = $('.contact-us-email').map(function() {
    return this.href.replace('mailto:', '');
    }).toArray();

and this is what appends the department name to the select element, which I think I could be doing wrong

        var together = department,
        only = email,
        filter2 = $(".contact-form-choose");
    $.each(together, function (index, value) {
        filter2.append($("<option>", {
            text: value,
            class: 'contact-email' + ++index
        }));
    });

and this is the last bit of code that puts the two arrays from up above together, though at the moment it isn’t really being put to use

  var departmentEmail = {};
    for (var i = 0; i < department.length; i++) {
        departmentEmail[department[i]] = email[i];
    }});

Read more here: Applying an email value to selected option on button click


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: