Using jQuery is the way to do go.
Since when you edit nodes it uses the admin theme (seven), we need to add the jQuery to the admin theme (seven).
Note: The default admin theme for Drupal 7 is "Seven", if you have changed your admin theme, then just edit that theme instead.
Go to the folder drupal/themes/seven
and edit seven.info
After stylesheets[screen][] = style.css
add the following line
scripts[] = cool.js
Create cool.js inside the seven folder (path would be drupal/themes/seven/cool.js
)
cool.js
(function ($) {
(function Forever(){ // we create a function called Forever
var grabselectedtext = $('#edit-field-test-und option:checked').text();
$('label').each(function(){
if ($(this).attr('for') == 'edit-body-und-0-value') {
if (grabselectedtext != '- None -') {
$(this).text(grabselectedtext);
} else {
$(this).text('Field Two');
}
}
});
setTimeout(Forever, 1);
})();
})(jQuery);
In this example the select list field is named test
and the field to change label is the body
field.
The above does exactly what you asked, but perhaps it might be even better (depending on your use case) if you hide the body
field if the test
field is equal to None. Then if the person selects something the body field reveals itself by sliding down.
cool.js (option 2)
(function ($) {
(function Forever(){ // we create a function called Forever
var grabselectedtext = $('#edit-field-test-und option:checked').text();
$('label').each(function(){
if ($(this).attr('for') == 'edit-body-und-0-value') {
if (grabselectedtext != '- None -') {
$(this).text(grabselectedtext);
$('#body-add-more-wrapper').slideDown(2000); // slides down in 2 seconds
} else {
$(this).text('Field Two');
$('#body-add-more-wrapper').slideUp(2000); // slides up in 2 seconds
}
}
});
setTimeout(Forever, 1);
})();
})(jQuery);
Then add this CSS to Seven's style.css
#body-add-more-wrapper { display: none; }
PS: Not a good idea to use the body field for this, better create your own custom field, otherwise all body fields will be hidden. I
didn't because I was lazy as this is just for example purposes.
Result:
