user3642210 user3642210 - 4 months ago 19
PHP Question

Adding onfocus and onblur within PHP

I have tried inputting the onfocus and onblue event within a form within PHP on a WordPress website, and cannot fathom the best way to do so:

Here is the form code within 'general'template.php' which is a WordPress file:

$form = '
<form name="' . $args['form_id'] . '" id="' . $args['form_id'] . '" action="' . esc_url( site_url( 'wp-login.php', 'login_post' ) ) . '" method="post">
' . $login_form_top . '
<input type="text" name="log" id="' . esc_attr( $args['id_username'] ) . '" class="input" value="Username" size="20" />
<input type="password" name="pwd" id="' . esc_attr( $args['id_password'] ) . '" class="input" value="Password" size="20" />
' . $login_form_middle . '
' . ( $args['remember'] ? '<label><input name="rememberme" type="checkbox" id="' . esc_attr( $args['id_remember'] ) . '" value="forever"' . ( $args['value_remember'] ? ' checked="checked"' : '' ) . ' /> ' . esc_html( $args['label_remember'] ) . '</label>' : '' ) . '
<input type="submit" name="wp-submit" id="' . esc_attr( $args['id_submit'] ) . '" class="button-primary" value="' . esc_attr( $args['label_log_in'] ) . '" />
<input type="hidden" name="redirect_to" value="' . esc_url( $args['redirect'] ) . '" />
<a class="btn btn-theme" href="/register/">Register</a>
' . $login_form_bottom . '
</form>';


I want to add the onfocus and onblur events to the Username and Password fields.

onfocus="if(this.value == 'Username') { this.value = ''; } " onblur="if(this.value == '') { this.value = 'Username'; } "


What would be the best way to go about this?

Thanks

Answer

You need to escape the single quotes using backslash. So, you need to use this way:

onfocus="if(this.value  == \'Username\') { this.value = \'\'; } " onblur="if(this.value == \'\') { this.value = \'Username\'; } "