0

Normally HTML check boxes are grouped based on name. Is it possible to group check box other than name?

Thank You!

4
  • 2
    checkboxes are independent of eachother, maybe you mean radios? then no only name. Commented May 18, 2010 at 15:27
  • 1
    @Galen: checkbox is multiple choice, radiobutton is single choice. The benefit of having the same name is that you've directly all selected values in an array in the server side in just one call. This way you don't need to grab them all together in multiple calls and then determine one by one if it's filled or not (i.e. null/empty or not). Commented May 18, 2010 at 15:28
  • 1
    @balusC: i know the difference. I wasnt sure the asker knew because of his question. hoping he would clarify Commented May 18, 2010 at 15:37
  • What would the purpose of this grouping be? It's apparent you don't want the checkbox values to be linked (grouped) together as an array. The only other kind of "grouping" that I can think of is grouping on a page for layout purposes, but the name of a check box doesn't influence where it's placed on the page, anyway. TL;DR: Please be more specific. Commented May 18, 2010 at 16:47

3 Answers 3

2

Not in HTML. But you can write some code logic in the server side which groups them based on some condition, e.g. a common prefix or suffix in the parameter name or even parameter value.

However, that's only clumsy. Just profit the benefit HTML you gave with the ability to group them by just the name. If you have a problem with that, then it needs to be fixed somewhere else, maybe with the element ID (to denote uniqueness in the document tree) or the element style class (to denote a common meaning in the document tree).

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

Comments

0

If you are using ASP.NET, you can use the MutuallyExclusiveCheckBoxExtender in the AjaxToolkit to group checkboxes together. It groups them together so that only 1 checkbox in the group can be checked at a time, much like radiobutton groups.

http://www.asp.net/ajax/tutorials/creating-mutually-exclusive-checkboxes-cs

Comments

0

You can try sort the added items in your way. In following example for an ASP.Net Checkboxlist i used a comparer to sort the items descending(change it in your way):

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim allChk As New ArrayList(Me.CheckBoxList1.Items)
        allChk.Sort(New SortComparer(False))
        Me.CheckBoxList1.Items.Clear()
        For Each item As ListItem In allChk
            Me.CheckBoxList1.Items.Add(item)
        Next
    End Sub

    Public Class SortComparer
        Implements IComparer
        Private ascending As Boolean

        Public Sub New(ByVal asc As Boolean)
            Me.ascending = asc
        End Sub

        Public Function [Compare](ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
            Return x.ToString.CompareTo(y.ToString) * DirectCast(IIf(Me.ascending, 1, -1), Int32)
        End Function
    End Class

Comments

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.