Bucks
Bucks

Reputation: 687

Programmatically created h:commandButton does not invoke onclick="processInput(id)"

Here is how I programmatically create a <h:commandButton>:

private HtmlCommandButton generateButton(String id,String label){
  HtmlCommandButton button = (HtmlCommandButton)app.createComponent
      (HtmlCommandButton.COMPONENT_TYPE);
  button.setId(id);
  button.setValue(label);
  button.setOnclick("processInput(id)");
  return button;
}

When the button is clicked, I need to execute the processInput(id) function. However, when I click that button, nothing happens.

Upvotes: 0

Views: 1074

Answers (1)

Avinash Singh
Avinash Singh

Reputation: 3787

Onclick function will add the processInput as a JavaScript function . You need to pass the id in your Java method like,

setOnClick("processInput(" + id + ")");

To add an action method you need to use setActionExpression,

HtmlCommandButton button = new HtmlCommandButton();
FacesContext fc = FacesContext.getCurrentInstance();
ELContext ctx = fc.getELContext();
String expression = "#{processInput('id')}";
Class[] parameterTypes = new Class[1];
parameterTypes[0]=String.class;
MethodExpression me = fc.getApplication().getExpressionFactory().
                      createMethodExpression(ctx, 
                      expression, returnType, parameterTypes);
button.setActionExpression(me);
button.setOnclick("alert('');"); 

Upvotes: 1

Related Questions