Sometimes you want to get selected value of radioButtonList using jQuery in your Yii powered application and it feels difficult due to Yii’s field naming conventions, as every field name contains array. Lets take an example. Suppose you have a radioButtonList as shown below and you want to enable/disable another field based on user selection from radioButtonList. We have added a JavaScript function onChange event of our radio buttons. Below code demonstrate how you can check selected value of radioButtonList using jQuery and perform conditional logic. <div class="row"> <?php echo $form->labelEx($model, 'nationality'); ?> <?php echo $form->radioButtonList($model, 'nationality', array( 'US' => 'US', 'Non US' => 'Non US', ), array( 'onChange'=>'toggleSSN();' )); ?> <?php echo $form->error($model, 'nationality'); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'ssn'); ?> <?php echo $form->textField($model, 'ssn', array('size' => 9, 'maxlength' => 9, 'disabled'=>true)); ?> <?php echo $form->error($model, 'ssn'); ?> </div> function toggleSSN(){ var nationality = $('input[name="User[nationality]"]:checked').val(); if(nationality == 'US') { $('#User_ssn').attr('disabled', false); } else { $('#User_ssn').attr('disabled', true); } }
Get Selected Value from Yii RadioButtonList
Sometimes you want to get selected value of radioButtonList using jQuery in your Yii powered application and it feels difficult due to Yii’s field naming
Related posts
JavaScript
nl2br equivalent JavaScript function
Learn how to create an <code>nl2br()</code> equivalent in JavaScript to convert newline characters into HTML line breaks. This guide covers simple JavaScript functions, PHP-to-JavaScript conversion, textarea handling, different newline formats, secure DOM methods, CSS alternatives, and important XSS considerations when working with user-generated content.
JavaScript
Style file upload field
Learn how to transform a standard HTML file upload field into a stylish and user-friendly upload control using jQuery File Style. This guide covers the required jQuery files, implementation steps, customization options, multiple file inputs, and important file upload considerations.
JavaScript
jQuery Alert Dialogs by Bill Beckelman
Learn how jQuery Alert Dialogs by Bill Beckelman can replace basic JavaScript alerts with customizable dialog boxes. This guide covers implementation, form validation, message types, benefits, and key considerations for using jQuery alert dialogs in modern and legacy web applications.