theme_checkbox

5 form.inc theme_checkbox($element)
6 form.inc theme_checkbox($element)
7 form.inc theme_checkbox($variables)
8 form.inc theme_checkbox($variables)

Format a checkbox.

Parameters

$element: An associative array containing the properties of the element. Properties used: title, value, return_value, description, required

Return value

A themed HTML string representing the checkbox.

Related topics

File

includes/form.inc, line 1889

Code

function theme_checkbox($element) {
  _form_set_class($element, array('form-checkbox'));
  $checkbox = '<input ';
  $checkbox .= 'type="checkbox" ';
  $checkbox .= 'name="' . $element['#name'] . '" ';
  $checkbox .= 'id="' . $element['#id'] . '" ';
  $checkbox .= 'value="' . $element['#return_value'] . '" ';
  $checkbox .= $element['#value'] ? ' checked="checked" ' : ' ';
  $checkbox .= drupal_attributes($element['#attributes']) . ' />';

  if (!is_null($element['#title'])) {
    $checkbox = '<label class="option" for="' . $element['#id'] . '">' . $checkbox . ' ' . $element['#title'] . '</label>';
  }

  unset($element['#title']);
  return theme('form_element', $element, $checkbox);
}

Comments

Confused

Can anyone explain to me why the input tag is within the label tag? Why is it not

$checkbox = '<label class="option" for="' . $element['#id'] . '">' . $element['#title'] . '</label>' . $checkbox ;

Other form elements have <label>Label Text</label><input ...

I wanted to style the label

I wanted to style the label when an input is checked. One way to do it without Javascript is using a CSS sibling selector: ".form-item input:checked + label".

To place the label after the input, you can use this version in your template.php:

<?php
function theme_checkbox($element) {
 
_form_set_class($element, array('form-checkbox'));
 
$checkbox = '<input ';
 
$checkbox .= 'type="checkbox" ';
 
$checkbox .= 'name="' . $element['#name'] . '" ';
 
$checkbox .= 'id="' . $element['#id'] . '" ';
 
$checkbox .= 'value="' . $element['#return_value'] . '" ';
 
$checkbox .= $element['#value'] ? ' checked="checked" ' : ' ';
 
$checkbox .= drupal_attributes($element['#attributes']) . ' />';

  if (!
is_null($element['#title'])) {
   
$checkbox .= '<label class="option" for="' . $element['#id'] . '">' . $element['#title'] . '</label>';
  }

  unset(
$element['#title']);
  return
theme('form_element', $element, $checkbox);
}
?>

^ ditto

^ ditto

Login or register to post comments