I have used Contact Form 7 to create a product enquiry form on each product page. The forms contains checkboxes for every product to help with the enquiry. How do I automatically set a specific product checkbox to true/on if the user is on the corresponding product page?
I’ve added a dynamic hidden field to the form which gets the page title.
Contact form &produce a checkbox array in the name field of each check box name="products[]"
Ideally i’d like the script to loop through and select checkbox that matches the hidden field, can somebody show me how to do this please?
Below is my current jquery
$( document ).ready(function() {
var get_product_name = $('#product-name').val(); // hidden field value
if(get_product_name == 'product-page-title')
{
$('CHECKBOX_HERE').prop('checked', true);
}
});
HTML produced by contact form 7…
<span class="wpcf7-form-control-wrap page-title">
<input type="hidden" name="page-title" value="Product template" size="40" class="wpcf7-form-control wpcf7dtx-dynamictext wpcf7-dynamichidden" aria-invalid="false">
</span>
<span class="wpcf7-form-control-wrap products">
<span class="wpcf7-form-control wpcf7-checkbox">
<span class="wpcf7-list-item first"><label><input type="checkbox" name="products[]" value="Product One"><span class="wpcf7-list-item-label">Product One</span></label>
</span>
<span class="wpcf7-list-item"><label><input type="checkbox" name="products[]" value="Product Two"><span class="wpcf7-list-item-label">Product Two</span></label>
</span>
<span class="wpcf7-list-item"><label><input type="checkbox" name="products[]" value="Product Three"><span class="wpcf7-list-item-label">Product Three</span></label>
</span>
<span class="wpcf7-list-item"><label><input type="checkbox" name="products[]" value="Product Four"><span class="wpcf7-list-item-label">Product Four</span></label>
</span>
<span class="wpcf7-list-item last"><label><input type="checkbox" name="products[]" value="Product Five"><span class="wpcf7-list-item-label">Product Five</span></label>
</span>
</span>
</span>
As you can see I have limited skills
2
Answers
UPDATE!! this worked!
Check hidden field value which contains page title
Checks array of checkbox values
Loop though checkbox values
Check if any checkbox values match the hidden field
Set that checkbox to true
The selector is not correct, you are trying to select an input element that has an id ‘product-name’ so the input you are searching for looks like this:
But you don’t have that in your HTML, so you should make a selector for this element which is the one you are looking for:
So option 1, is to change the selector $(‘#product-name’).val(); for this selector:
But this is too generic, doing so could select more than 1 input type hidden and give you a different value if you want something more specific you could use the existing class from the html element:
And then call the selector like this:
Again this is also really generic, and it could select all fields in your page that has a class wpcf7-dynamichidden, so the best approach is if you could change your HTML to have a specific id or a specific name, or a specific class, in which you could change later your selector $(‘#product-name’).val() to something like this:
Afterall if you just want to try out this code, it could work:
Hope this helps.