Saif Navid
Saif Navid

Reputation: 55

How to change HTML input type of label using Java or wicket

I wanted to know how I could change the input type of my Password field from "password" to "text" so that password no longer shows up as asterisks. I'm using Java and wicket in my project.

<div class="loginPassword">
    <label for="password"><wicket:message key="login.password"/></label>
    <input class="ft_login_password" wicket:id="password" type="password" name="password" />
</div>

The password field is set in my Java code as a PasswordTextField:

import org.apache.wicket.markup.html.form.PasswordTextField;
PasswordTextField passwordField = new PasswordTextField("password");

Upvotes: 0

Views: 478

Answers (1)

martin-g
martin-g

Reputation: 17513

As explained in the comments you should do this with JavaScript on the client side.

  1. there is no point in making Ajax request to make the change on the server

  2. Wicket will complain that PasswordTextField expects the type attribute to be password: https://github.com/apache/wicket/blob/837f3c137bf39f26ddc3b8e939235cde04e8c13d/wicket-core/src/main/java/org/apache/wicket/markup/html/form/PasswordTextField.java#L115

If you insist on doing it with Java then you will have to override PasswordTextField#getInputTypes() to return new String[] {"password", "text"}.

With Wicket/Java you should do something like:

passwordField = new PasswordTextField(...);
passwordField.setOutputMarkupId(true);

new AjaxLink<Void>("passwordToText") {
  @Override public void onClick(AjaxRequestTarget target) {
    passwordField.add(AttributeModifier.replace("type", "text"));
    target.add(passwordField);
  }
}

Upvotes: 0

Related Questions