1

Let's suppose: a page with some dynamically generated checkboxes outside of a form element.

After the user has checked some of the checkboxes, I would love to append all those checkboxes (either checked or unchecked) into the form element so that when the user click the "submit" button, the form takes into account the checkboxes, their ids, names, data-names and their status (checked or unchecked). Is that possible ?

I have tried a codpen here: https://codepen.io/anthonysalamin/pen/ZxvZpP?editors=1010 but was unsuccessful sofar.

The jQuery code:

//insert all checkbox input elements into the form id="reservation"
  $("#reservation").submit(function(evt) {
  // should append all checkboxes to the form
  $("<input type='checkbox' />").append("#reservation");
});

Screenshot of my codpen here

1
  • I guess my question would rather be: adding multiple checkboxes id's, names and values to a form element. I do not actually want any "physical" checkboxes to appear into my form element, I just wish to send all the checkboxes informations through the form' submit button. Commented Mar 28, 2018 at 16:11

1 Answer 1

0

.append() expects textual HTML as input. You have passed the id selector of #reservation instead. To effectively add the checkboxes to the form in the submit event you could do this:

// wait for DOM to be ready
$( document ).ready(function() {
    //define the variable for all checkboxes on the page
    var allCheckboxes = $("input:checkbox[class=outsider]"); 
    //calls the append function on form submission
    $("#form-element").submit(function(event) {
        //insert all checkbox type elements into the form with id="form-element"
    var checkHTML = "";
    for(var checkboxIndex = 0; checkboxIndex < allCheckboxes.length; checkboxIndex++) {
      checkHTML += allCheckboxes[checkboxIndex].outerHTML;
    }
        $("#form-element").append(checkHTML);
    });
});

However, it is highly probable that your intention differs from the behavior. Problems with the code:

  • your checkboxes have ids and if we clone them, then you will have duplicate ids. Since an HTML cannot be valid if there are duplicate ids, cloning the checkboxes with ids will result in invalid HTML
  • checked state is not copied, so you will need to mine that value out and put it to the newly created checkboxes

Code dealing with all these problems:

// wait for DOM to be ready
$( document ).ready(function() { 
    //define the variable for all checkboxes on the page
    var allCheckboxes = $("input:checkbox[class=outsider]"); 
    //calls the append function on form submission
    $("#form-element").submit(function(event) {
        //insert all checkbox type elements into the form with id="form-element"
    var checkHTML = "";
    var checked = [];
    for(var checkboxIndex = 0; checkboxIndex < allCheckboxes.length; checkboxIndex++) {
      checked.push($(allCheckboxes[checkboxIndex]).prop('checked'));
      allCheckboxes[checkboxIndex].remove();
      checkHTML += allCheckboxes[checkboxIndex].outerHTML;
    }
        $("#form-element").append(checkHTML);
    allCheckboxes = $('input:checkbox[class=outsider]');
    console.log(checked);
    for (var checkboxIndex = 0; checkboxIndex < allCheckboxes.length; checkboxIndex++) {
      $(allCheckboxes[checkboxIndex]).prop('checked', checked[checkboxIndex]);
    }
    event.preventDefault();
    });
});

So you can solve your problem by cloning the checkboxes if you handle all the problems. Alternatively, you could use a hidden input where you could put key-value pairs, where the keys will be checkbox ids and values will be their corresponding checked states and put those values into the hidden input at the submit handler. If you do not specifically intend to visually put the checkboxes into the form and you are satisfied with correct handling of data, then putting the JSON value of key-value pairs into the value of a hidden input is superior in comparison of copying checkboxes into the form, but these are only nuances.

Sign up to request clarification or add additional context in comments.

5 Comments

thank you so much for your script and the time taken to write it, really appreciate ! It worked beautifully in my Webflow project with static elements (checkboxes and form), but when I tried with dynamic elements, it unfortunately didn't work anymore eventhough I used the dynamic fields to replace the form and checkboxes IDs. I have to give up this project, I'm lost - I have asked so many questions around also on the webflow forum but no one could really help me out. Is it maybe a back-end issue I can't handle with code ?
Should you have interest and time, feel free to have a look at the webflow post I did link. I then used your updated code but there too, worked on the static page but not on the dynamic one.
@anthonysalamin I have looked into your link and read some posts, but unfortunately I do not have the time to read all posts. Since I have only partial knowledge about your actual problem, I cannot determine whether I am factually right, but there could be two possible problems as I see it: 1. Your form is inside an iframe and your code runs outside of it and therefore does not find your form and checkboxes. 2. You might have a Javascript error. Anyway, first things first: please check if you have any Javascript errors in your project using the browser's web tools and tell me what you see.
Hi @lajosarpad, really appreciate you've taken the time to have a look at my link ! I did solve my problem now and I am really grateful ! I've learned quite a bit with your technique ! :)
@anthonysalamin you are welcome. I am happy you have managed to solve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.