lundi 5 octobre 2015

Copy Checkbox Labels to a Hidden text field

This is my very first SO question, os my apologies if I mess anything up here. I've searched high and low but can't find the exact thing I'm looking for.

I'm having some issues getting a script provided to me to do what I'd like it to do. Essentially, I'd like all the labels of checked checkboxes with a parent of a certain class to be created as a string separated by semicolons and added to a hidden field.

Here's the function I'm working with:

        function copyProductInterest() {
            var resultData = '';

            var listLabels = document.querySelectorAll('.Pardot_In_Scope .inline');
            var listInputs = document.querySelectorAll('.Pardot_In_Scope input');
            for( var i=0; i<listInputs.length; i++){
                label = listLabels[i];
                checkbox = listInputs[i];
                if( checkbox.checked ){
                    resultData += label.innerHTML + ';';
                }
            }
            document.querySelectorAll('Test_Comment input')[0].value = resultData;
            return true;
        }

The form in question is here - http://ift.tt/1FTUTJL. I'd like the labels (not the values) of any of the Pardot In Scope checkbox fields to be mapped to the Test Comment field (currently not hidden) as an array/string separated by semicolons.

I'm calling the function this way...

<form accept-charset="UTF-8" method="post" action="http://ift.tt/1FTUTJL" class="form" id="pardot-form" onsumbit="copyProductInterest()">

If this could be done as the boxes are (un)checked, that would be great, however onsubmit would be fine as well. As it is, I'm not getting anything.

I don't have access to either name or style the checkbox fields, so I'm trying to target them through the parent p's class (Pardot_In_Scope). There's not much else I can do from an HTML standpoint, either, because this form is generated through Pardot and there isn't a ton of access they give you.

The purpose of this whole thing is to push the values through a web-to-lead form on the back-end of the Pardot form. I know that an array separated by semicolons will properly map on the other end. I just can't fill in the gap between the code I was provided and why it's not working for me.

Any help would be greatly appreciated! Also, Pardot does not allow server-side scripts, which is why I'm looking at JavaScript.




Aucun commentaire:

Enregistrer un commentaire