Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a <a href="http://jquery.com" rel="nofollow">jQuery</a> solution, I'll add a plain JS one in a few moments.</p> <pre><code>$('form[name="form1"]').on('submit', function(e) { if(!$('#filters input:checkbox:checked').length) { e.preventDefault(); alert('Please select at least one filter.'); } }); </code></pre> <p>This codes does <strong>not</strong> require the <code>onsubmit</code> inline event.</p> <p>Since you are not familiar with jQuery I'll explain it more thoroughly:</p> <ul> <li><code>$('form[name="form1"]')</code> creates a jQuery object containing all elements matching the selector. It would be faster if you gave your form <code>id="form1"</code> and used <code>$('#form1')</code></li> <li><a href="http://api.jquery.com/on/" rel="nofollow"><code>.on()</code></a> binds an event handler. The first argument passed to the callback function is a wrapped event object which we'll need to prevent the form from being submitted if necessary.</li> <li><code>$('#filters input:checkbox:checked')</code> selects all checked checkboxes that are children of <code>#filters</code>. <code>:checkbox</code> and <code>:checked</code> are pseudo-selectors which will only match checkboxes that are currently checked)</li> <li><code>.length</code> is the number of elements in the jQuery object - if nothing is checked it's zero</li> <li><a href="http://api.jquery.com/event.preventDefault/" rel="nofollow"><code>e.preventDefault();</code></a> prevents the default action of the event from being executed, i.e. the form will not be submitted.</li> </ul> <p>Usually you would wrap the whole code with <code>$(document).ready(function() { ... });</code> to make it execute as soon as the DOM is ready - if you put the <code>&lt;script&gt;</code> tag containing the code after the <code>&lt;/form&gt;</code> tag of your form, it's not necessary though.</p> <hr> <p>If you really want a plain JS solution, try this:</p> <pre><code>function Validate() { var container = document.getElementById('filters'); var checked = 0; for(var i = 0; i &lt; container.childNodes.length; i++) { var elem = container.childNodes[i]; if(elem.tagName == 'INPUT' &amp;&amp; elem.type == 'checkbox' &amp;&amp; elem.checked) { checked++; } }; if(checked) { return true; } alert('Please select at least one filter.'); return false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload