By Fiasst on
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
Ah, Just as I posted this I
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:
Use double quotes
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.
Not able to set password default value
hi,
It works but not able to set password default value
No solution?
Has anyone found a solution for that? I've got the same problem and I can't figure out how to solve it..
create a variable
A simple solution that worked for me, is to create a variable to hold the offending '' part of the js
e.g
You could even put the whole js onclick string into the variable.
hook_form_alter or jquery
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
Alternate solution
Mistaken post.
Alternate solution
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(
function test() { alert("test"); }'#type' => 'button',
'#value' => t('Test'),
'#prefix' => '
',
'#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' => '',
);
Alternate solution continued
Not sure what the security implications are for this alternative so use at your own risk.
text module issue
If you using text module for that (CCK), check this issue:
#470260: #attributes are not properly rendered
Good, it works
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.
any fix to this?
I tried all the suggestions posted here, but none worked. Always rendering as &, etc., to the page. Thanks,
?
Similar
I arrived at a similar solution when working on a search block form:
Works perfectly fine
This works perfectly fine for me.
Thank you for this resolution.
Add onClick attribute to login form textfield?
this issue is not working in drupal 6 please help
Not working for me either in
Not working for me either in Drupal 6
Imran Khan
Project Manager
New Earth Marketing