6

Is there any way to change a <p:commandButton> value with Javascript?

I tried to set a widgetVar in the <p:commandButton> but it doesn't have any setValue() function or something like this. I can't bind it to a managed bean due to other specific issues with my XHTML.

2 Answers 2

6

JSF generates HTML. JavaScript works with HTML. When writing JavaScript code, don't look too much at the JSF source code if you already can't tell form top of head what HTML code exactly it would generate.

Open the JSF page in your favourite browser, rightclick and View Source. The generated HTML output of a <p:commandButton value="foo" /> look like this:

<button id="formId:buttonId" name="formId:buttonId" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" onclick="PrimeFaces.ab({formId:'formId',source:'formId:buttonId',process:'@all'});return false;" type="submit">
    <span class="ui-button-text">foo</span>
</button>

Look, the button has a single <span> child with therein the value.

So, this piece of JS to change the button's value to bar should do:

document.getElementById("formId:buttonId").firstChild.innerHTML = "bar";
Sign up to request clarification or add additional context in comments.

1 Comment

I see, that's what I did before but thought there was a way to deal with its WidgetVar. Thanks a lot for your help.
1

This is a better way using the widgetVar :

lets say we set confirmWgt as the <p:commandButton> widgetVar, The javascript code to set its value will be :

PF('confirmWgt').jq.text("New Value");

Using this way, you are sure if any changes in the html code applied by primefaces in the future will not cause any problem in updating its version.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.