0

I found the following post (textbox must not be empty if checkbox is ticked) and it relates to what I want to do with my page. But how do you make this work if the checkboxes and texboxes are generated dynamically so you don't know the final number that will be present? Sometimes I have 3 checkbox and textbox pairs and other times I have 25 or more. How is this done with jquery?

I created this fiddle at https://jsfiddle.net/v2vzs3ew/ with code for html and jquery. After reading quite a bit about jquery and looking at many different examples I came up with this but am not sure why it does not work, at least partially.

$(document).ready(function() {
  $("#button").click(function() {
   if ($(".chk").each(":checked") && $(".parcel").val().length <= 0)
    alert("You must input acres for all parcels checked.")
  });
});

1 Answer 1

1

There are multiple ways to grab all checkboxes and textboxes using JQuery.

A simple solution for checkboxes, off the top of my head, would be to use the :checkbox selector with an .each() loop. This will let you do whatever you want with every checkbox on the page.

$(':checkbox').each(function() {
    //$(this) is your checkbox object
});

For a full list of jQuery selectors, you can find them at http://api.jquery.com/category/selectors/

There are a ton of different ways jQuery can handle your situation, so do some research or post some code examples if you have a more specific scenario.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.