Hello,

I'm trying to add onClick and onBlur attributes to my Login form input textfields. I have no idea how to achieve this but I found '#attributes' => array( shown in the below code. It's probably not the way to do it and I'd really appreciate some help!

Thanks.

function user_login_block() {
  $form = array(
    '#action' => url($_GET['q'], drupal_get_destination()),
    '#id' => 'user-login-form',
    '#base' => 'user_login',
  );
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('Username'),
    '#maxlength' => USERNAME_MAX_LENGTH,
    '#size' => 15,
	'#value' => t('Username'),
    '#required' => TRUE,
	'#attributes' => array(
    	'onblur' => 'clickclear(this, 'Username')',
    	'onclick' => 'clickrecall(this, 'Username')'),
  );

Comments

Fiasst’s picture

Ah, Just as I posted this I found my way! BUT, by using " " in the code below, viewing the page source I get:
onblur="clickrecall(this, "Username")" onclick="clickclear(this, "Username")"

Question: How do you use quote marks with php when already inside quote marks? in Javascript I beleive you add a forward slash like: 'clickrecall(this, /'Username/';)'

Thanks!

The code so far:

function user_login_block() {
  $form = array(
    '#action' => url($_GET['q'], drupal_get_destination()),
    '#id' => 'user-login-form',
    '#base' => 'user_login',
  );
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('Username'),
    '#maxlength' => USERNAME_MAX_LENGTH,
    '#size' => 15,
    '#required' => TRUE,
	'#attributes' => array(
    	'onblur' => 'clickrecall(this, "Username")',
    	'onclick' => 'clickclear(this, "Username")'),
	'#value' => t('Username'),
  );
  $form['pass'] = array('#type' => 'password',
    '#title' => t('Password'),
    '#maxlength' => 60,
    '#size' => 15,
	'#attributes' => array(
    	'onblur' => 'clickrecall(this, "Password")',
    	'onclick' => 'clickclear(this, "Password")'),
	'#value' => t('Password'),
    '#required' => TRUE,
  );
drbutts’s picture

Just had this problem and figured it out.

Use 'onclick' => "clickclear(this, 'Username')"

Note the reversal where single quotes are double quotes, and vice versa.

SonamSdiwate’s picture

hi,
It works but not able to set password default value

ideveloper’s picture

Has anyone found a solution for that? I've got the same problem and I can't figure out how to solve it..

BobbyG123’s picture

A simple solution that worked for me, is to create a variable to hold the offending '' part of the js
e.g

////////////////////////////
//var to get around '' issue
		$div1= "'div1'";

  'onclick' => 'showMe('.$div1.', this)'),


You could even put the whole js onclick string into the variable.

jswaby’s picture

your 2 options are to either build a simple module that implements hook_form_alter and add it in:

http://api.drupal.org/api/HEAD/function/hook_form_alter

or if you're using drupal 5, do a jquery equivalent to what you want to do.

======
Jason

jdrichmond’s picture

Mistaken post.

jdrichmond’s picture

I tried some of the solutions for adding javascript into the #attributes with no success. I tried an alternative solution by adding the javascript to #prefix like the following, but it didn't work either.

$form['test'] = array(
'#type' => 'button',
'#value' => t('Test'),
'#prefix' => '

function test() { alert("test"); }

',
'#attributes' => array('onclick' => 'javascript: test();'),
);

This worked to an extent... It will display the javascript correctly without changing the quotes to html, but will not work for a redirect. The form gets re-render so if the data does not validate than the user is brought back to the form. If you don't want the user to leave the form without filling out the form fields than this is a acceptable solution. In order to get a redirect working I had to create a button outside of the form using the markup type.

$form['previous'] = array(
'#prefix' => '',
'#value' => '',
'#suffix' => '',
);

jdrichmond’s picture

Not sure what the security implications are for this alternative so use at your own risk.

kenorb’s picture

If you using text module for that (CCK), check this issue:
#470260: #attributes are not properly rendered

javilete’s picture

Hi, I have been taking a look and searching some information about how to add events in a field of a form. Everything is done fine but I dont know how to add the function. For example, in the last post, where do you have the function "clickrecall()"?
Thanks a lot in advance.

getstarted’s picture

I tried all the suggestions posted here, but none worked. Always rendering as &, etc., to the page. Thanks,

b.tomi’s picture

    '#default_value' => t('Username'),
    '#attributes' => array(
        'onblur' => 'this.value="Username"',
        'onclick' => 'this.value=""'),
jsims281’s picture

I arrived at a similar solution when working on a search block form:

    $form['search_block_form']['#attributes'] = array(
      'onfocus' => "if (this.value == 'Enter Search') {this.value = '';}",
      'onblur' => "if (this.value == '') {this.value = 'Enter Search';}"
    ); 
vyasamit2007’s picture

This works perfectly fine for me.

Thank you for this resolution.

SonamSdiwate’s picture

this issue is not working in drupal 6 please help

khan2ims’s picture

Not working for me either in Drupal 6

Imran Khan
Project Manager
New Earth Marketing