1

I can't seem to attach an event to my struts radio button.

It's defined as:

<s:radio id="useCourseList"
         name="useCourseList"
         required="true"
         label="Course List?"
         labelposition="top" 
         list="#{'1':'No','2':'Yes'}" 
         value="1" 
/>

But I can't seem to create a click event for it. I tried

$('#useCourseList').click(function(event){
    if($(this).val() === '2') {
        renderCourseList();
    } else {
        switchToSingleCourseMode();
    }
});

But that attaches to "useCourseList" - the actual ids of the buttons, though, are "useCousreList1" and "useCourseList2".

It seems wonky to assign the same click event to both of them, especially since when more options are added later, I'll have to add to those too. How can I add "one and done"?

Jonas
  • 121,568
  • 97
  • 310
  • 388
corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • May you show what is the generated HTML instead? – Alexander Jan 07 '13 at 22:09
  • If the actual IDs are different than the one you use in the jQuery selector, of course it won't fire. Instead, add class attribute to your element, and select by class. –  Jan 07 '13 at 22:10
  • 1
    Give the radio buttons a CSS class and use that instead. – Dave Newton Jan 07 '13 at 22:54
  • @Dave great - now I'm torn... You see, after reading your suggestion I think it's the right thing to do. But I have a solution up and running. So... do I go with the solution that's in my code already and working? Or do I step back and do it right. Gar! – corsiKa Jan 07 '13 at 22:59
  • Not really much of a change. – Dave Newton Jan 07 '13 at 23:34

1 Answers1

1

I'm assuming that the rendered html is not going to have the id exactly as it is defined. You can try:

$('[id*="useCourseList"]').click(...

see http://api.jquery.com/attribute-contains-selector/

Justin Bicknell
  • 4,804
  • 18
  • 26
  • @OP: Reference for selector used: http://api.jquery.com/attribute-contains-selector/ –  Jan 07 '13 at 22:12