hey all, following situation:

i created a modul which cleared onfocus the default value of a textfield and onblur the default value occurs back...

This is my Module (setdefault.module)

<?php
function setdefault_form_alter(&$form, $form_state, $form_id){
  drupal_set_message('Id: "'. $form_id .'"');
  switch ( $form_id ) {
    case 'webform_client_form_53':
      $form['text']['#attributes']['onblur'] = 'if (this.value == "") {this.value = "'. $form['text']['#default_value'] .'";}';
      $form['text']['#attributes']['onfocus'] = 'if (this.value == "'. $form['text']['#default_value'] .'") {this.value = "";}';
      break;
  }
}
?>

My webform has the following id: webform_client_form_53

If i had installed it - nothing happend.

Anyone an idea?

*sry 4 my english

Comments

Mark Theunissen’s picture

I'm not sure what's wrong with your particular approach, but it is the "wrong" way i.e. it's not the Drupal way of doing this. You should add your Javascript using a call to drupal_add_js() and you should use jQuery to assign behavior to elements.

zubl’s picture

hmm everyone tell other things .. i had read this page hook_form_alter ... how i make it in java script - i have no idea... :( :( :(

Mark Theunissen’s picture

Where did you read the other things? Can you provide links?

zubl’s picture

yes surely but it's on german klick

i don't know if it helps but this is the code which i can include in the form.inc :

$output .= '<input type="text"'. $maxlength .' name="'. $element['#name'] .'" id="'. $element['#id'] .'"'. $size .' value="'. check_plain($element['#value']) .'"'. drupal_attributes($element['#attributes']) .' onblur="if(this.value== \'\') {this.value = \''. check_plain($element['#value']) .'\';}" onfocus="if(this.value == \''. check_plain($element['#value']) .'\') {this.value = \'\';}" />';

if i make it, it will works!!! but i don't want to change the core files :-/

grobemo’s picture

I looked at Google's translation of your the German page.

Do you see the Drupal message with the form ID? If not, then the problem is that the function isn't being called at all. I assume that you are seeing the message.

One problem with the module code is that you can only change specific fields in hook_form_alter, not all text fields. So, it should look like this:

<?php $form['NAME-OF-FIELD']['#attributes']['onblur'] = ... ?>

But as Mark suggested, a better approach is to include some JavaScript to your this. Some code like this should work:

Drupal.behaviors.restoreTextfieldDefaults = function(context) {

  $(context).find(':text:not(.restoreTextfieldDefaults-processed)').each(function() {
    $(this).data('defaultValue',$(this).val())
      .focus(function() { if ($(this).val() == $(this).data('defaultValue')) { $(this).val(''); } })
      .blur(function() { if ($(this).val() == '')) { $(this).val($(this).data('defaultValue')); } });
  }).addClass('restoreTextfieldDefaults-processed');

};

I haven't tested this code, but I think it will clear all textfields on focus (if they contain the default value) and fill them with the default value again on blur (if they're blank or empty).

zubl’s picture

ok many thanks ... and the code you've posted should i include in which file? or how can i include the code? *sry 4 asking .. i'm newb in things like module development :-/

grobemo’s picture

There are many ways to include JavaScript in a Drupal page. For your purposes, I'd recommend using drupal_add_js.

Save the JavaScript to a file in your module directory. Call it something like restore_text_defaults.js.

Then, include the following function in your module:

<?php

function YOUR-MODULE-NAME_form_alter(&$form, $form_state, $form_id) {
  drupal_add_js('restore_text_defaults.js','module','header',false,true,false);
  // drupal_add_js('restore_text_defaults.js') would work, too. See below.
}

?>

I'm calling drupal_add_js from hook_form_alter so that Drupal includes restore_text_defaults.js if and only if it's generating a form. (That's why the last argument of drupal_add_js is false. You probably don't want to include it on every page as part of the aggregated JS if you turn JavaScript optimization on.) If you only wanted this functionality on certain forms, you could even use <?php if (in_array($form_id,array('form-id-1','form-id-2','form-id-3')) { } ?> to do so.

earthangelconsulting’s picture

if there's any chance of the form failing validation, and having to be redisplayed, i wouldn't recommend putting drupal_add_js calls in a form_alter hook. see http://drupal.org/node/322290 Essentially, if you are using _form_alter hook (or the _form hook, in a node module) the form is cached and if it is redisplayed, it does NOT re-execute any of the drupal_add_js calls, and your javascript won't be there when redisplayed. there are various solutions recommended to this (see http://drupal.org/node/322290), when i find the best one, i will let you know ;-)

as far as i can tell... if you are building a form from scratch, and it's not a node form, then it won't be cached, and this won't be a problem.

cheers
Peter 'Fish' Fisera
GoatVirus Technologies

Mark Theunissen’s picture

Or take a look at the code here:

http://drupal.org/node/246730#comment-1091805

Mark Theunissen’s picture

zubl’s picture

hmm i created a JS File called test.js

Drupal.behaviors.restoreTextfieldDefaults = function(context) {

  $(context).find(':text:not(.restoreTextfieldDefaults-processed)').each(function() {
    $(this).data('defaultValue',$(this).val())
      .focus(function() { if ($(this).val() == $(this).data('defaultValue')) { $(this).val(''); } })
      .blur(function() { if ($(this).val() == '')) { $(this).val($(this).data('defaultValue')); } });
  }).addClass('restoreTextfieldDefaults-processed');

};

I saved it in the module folder...

After this i paste the following lines in the webform.module file

<?php

function webform_form_alter(&$form, $form_state, $form_id) {
  drupal_add_js('test.js','module','header',false,true,false);
}

?>

Nothing happens ...

please help me .. i'm going crazy!!!

selinav’s picture

have you find the solution for your problem.

I have a hook form alter module but I don't success to call javascript.

What is the exact syntax when your js file is in your module?

Lobbi.es’s picture

function webform_form_alter(&$form, $form_state, $form_id) {
drupal_add_js(drupal_get_path('module', 'the_name_of_your_module') . '/test.js','module','header',false,true,false);}