By johnhelen on
Hi
I have a form field like this
==========
$form['from'] = array('#type' => 'textfield',
'#title' => t('From'),
....
==========
I can see the form field in html source:
<div class="form-item" id="edit-from-wrapper">
<label for="edit-from">From: </label>
....
Is there any simple way so I can add a class or id to this label (so I can change the label text using Javascript )
What I want is something like this in html:
<div class="form-item" id="edit-from-wrapper">
<label id="label_id" for="edit-from" >From: </label>
Can I do it directly in the module or I have to do something in the theme
Many thanks
john
Comments
Go through following
Go through following link:
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
It explains everything about drupal forms.
Hope it helps.
It looks like that I CAN NOT
It looks like that I CAN NOT add an id for label in a form in module. That values is includes/form.inc file
Add a theme function
You can do this by adding a function in your theme template php, like this:
I just copied the function from the /includes/form.inc, edited the function name to match my theme (in the code above, change "yourthemename" to the name of your theme), and -- VOILA! -- a new class has been added to the label! I also added a class named "normal" so there is a general selector to differentiate these labels from labels for radio or checkbox elements.
Be sure to clear your theme registry in order to see the change.
Enjoy!
Anne
You don't need to add an ID
You don't need to add an ID to the label at all. Just target it in your javascript using:
$("#edit-from-wrapper label")(assuming you are using jquery).
Since IDs are unique, and form elements only have one label, the above selector will target that label and only that label.
Contact me to contract me for D7 -> D10/11 migrations.
Should be possible to add a
Should be possible to add a class to label.
To be compatible with bootstrap horizontal form for example.
Hi, you don't need to use
Hi, you don't need to use javascript for changing the field label.
you can use hook_form_alter in your custom module.
Refer the link http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hoo...
Use JS
Hi Johnhelen,
Ideally we can't add class or id in form label. but it can be easily done using javascript.
Using following code:
----- START -----
jQuery( document ).ready(function() {
jQuery( "label" ).addClass( "New class name" );
});
---- END ------
Best!
GMEET
want to hide the label?
For those looking to simply hide the label, like I was, you can use the #title_display property set to
'invisible'.