0

I am trying to parse through a typical form using jquery. I want to obtain all details about the various fields in the form- viz field name, field type (eg radio/checkbox/list)...

How do I determine if a field allows multiple values to be selected? (eg in check box or list box)?

If this cannot be done using jquery, then can it be done using pure javascript? I already have with me a ref to the element for which this (whether multiple values are allowed or not) has to be determined...

Arvind
  • 6,404
  • 20
  • 94
  • 143
  • If you have a set of checkboxes, you can always select multiple values, unless it is somehow prevent through JavaScript (though radio buttons would be more appropriate then) and I don't think you will be able to find that out (you could simulate selecting the boxes though and see how many are really selected). For a `select` list, check whether it has the `multiple` attribute. – Felix Kling Nov 04 '11 at 10:04
  • @FelixKling - pls put your comment as an answer. Thank you for your help. – Arvind Nov 04 '11 at 10:36

1 Answers1

1

Surround your control with some <div> as:

<div id="testCheck">
                <input type="checkbox" checked="checked" value="1" />
                <input type="checkbox" checked="checked" value="2" />
                <input type="checkbox" checked="checked" value="3" />
                <input type="checkbox" checked="checked" value="4" />
                <input type="checkbox" checked="checked" value="5" />
                <input type="checkbox" checked="checked" value="6" />
                <input type="checkbox" checked="checked" value="7" />
                <input type="checkbox" checked="checked" value="8" />
    </div>

and check the selected or unselected elements using follwwing jquery code snippet. .size() will return the no of checked item and you can get the value of selected items by .val().

//Write your code when Document is loaded or some element click

 $(document).ready(function() {
                $("#testChk").click(function() {
                    alert($("#testCheck :checked").size());
                    //function to print the value of each checked checkboxes
                    $("#testCheck :checked").each(function() {
                        alert("value = " + $(this).val());   
                });

for more help follow these links:

How can I know which radio button is selected via jQuery?

have a look on jquery selector. you should watch bottom itms :password, :reset etc that you need to use. http://api.jquery.com/category/selectors/

http://jquery-howto.blogspot.com/2008/12/how-to-check-if-checkbox-is-checked.html

Handling Checkboxes, Radio Buttons and Select Options in jQuery

Hope this help you little bit...

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • @NiranjanKala- thank you for the exhaustive and well researched answer you have provided...unfortunately I have no control over the div... Hence I will have to settle for the answer provided by Felix Kling. Thank you though for your help... – Arvind Nov 04 '11 at 13:10