Aldo Paradiso
Aldo Paradiso

Reputation: 931

Adding CSS class to form element in Moodle

I create forms in Moodle by extending the moodle moodleform class and calling the addElement() or createElement() methods. For example, I create a select element this way:

$mform->addElement('select', 'mySelect',  'Select title' , $list);

Where $mform is a reference to the form, and $list is an associative array like:

$list = array('1'=>'one', '2'=>'two', '3'=>'three').

The question is: Is there a way to add a CSS class associated with this or other form elements?

I other words, I would like to programmatically add a class myClass, so that the HTML code looks like this:

<select class="myClass">
  <option value="1">one</option>
  <option value="2">two</option>
  <option value="3">three</option>
</select>

Upvotes: 2

Views: 4139

Answers (1)

davosmith
davosmith

Reputation: 6317

Use the 5th addElement parameter - https://docs.moodle.org/dev/lib/formslib.php_Form_Definition#select

$mform->addElement('select', 'myselect', 'Select title', $list, ['class' => 'myclass']);

(And I assume your real code does this correctly, but make sure you use get_string() for param 3, so it can be translated).

Upvotes: 4

Related Questions