jQWidgets Forums
jQuery UI Widgets › Forums › Editors › Button, RepeatButton, ToggleButton, LinkButton › ButtonGroup getSelection error
Tagged: getSelection, groupButton, JavaScriptError
This topic contains 4 replies, has 2 voices, and was last updated by KBTibbs 11 years ago.
-
Author
-
In it’s default state, the ButtonGroup has no buttons selected. In Radiobutton mode, this causes a javascript error when getting the selected value. This default case should be handled gracefully in the library by either catching the case and returning an appropriate value or by forcing a selection when entering RadioButton mode.
Reproduction steps:
1) Go to http://www.jqwidgets.com/jquery-widgets-demo/demos/jqxbutton/buttongroup.htm
2) Select “RadioButtons” mode
3) Attempt to get the selection via javascript with the following command
$("#jqxWidget").jqxButtonGroup("getSelection")
Note the Javascript errors
“TypeError: Cannot read property ‘num’ of null” – Chrome 35
“Unable to get value of the property ‘num’: object is null or undefined” – Internet Explorer 9
“TypeError: b._getSelectedButton(…) is null” – Firefox 25Hi,
Here’s a sample: http://jsfiddle.net/jqwidgets/dbkHp/ which illustrates how this should be used.
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.com/Are you telling me that an unhandled error is expected and desired behavior in your library?
Hi,
No, as far as I know, I wrote you how to use the method.
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.com/And I wrote to you about the very real use case of checking the value outside of the element’s click event and how it can fail.
Can you name another JQWidget that will catastrophically fail if JQX commands are run before user interaction? (Because that’s not the very *best* of paradigms.)
Default mode handles this use case by returning undefined:
RadioButton Mode handles this use case by returning an empty array:
Checkbox Mode has a fatal error, which I (as a user of this library) cannot test or account for without modification of your library code.
Leaving aside the completely inconsistent manner of return types (Sometimes it’s an int, sometimes an array, sometimes a string), calling a method on a variable that could be null without handling that case is just bad programming. Now, if you have a git repo I would gladly fork it, add the two lines of code to handle a null value, and send you a pull request.Otherwise:
Which, I think, is a decent solution. Not only does it not error, it consolidates expected return types. This makes it easier for those of us using your library. Honestly, I would prefer that this control always return an array, but the posted solution above is literally the least amount of effort required to fix this error.
-
AuthorPosts
You must be logged in to reply to this topic.