Execute a CommandButton using JavaScript

UI Components for JSF
Post Reply
robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

06 Mar 2011, 19:39

Hello everybody.

Is there a way to "click" a <p:commandButton> using JavaScript? The User's Guide only lists the enable() and disable() functions for the Client side API.

Thanks in advance for any advice!
Best regards,
Robert

User avatar
ydarcin
Posts: 258
Joined: 04 Jan 2009, 19:02
Location: Turkey

06 Mar 2011, 21:50

hi,

instead of clicking the p:commandbutton, you can use remote command maybe.

http://www.primefaces.org/showcase/ui/remoteCommand.jsf

btw I remember with jQuery click(), you can do what you want.

Yigit

healeyb
Posts: 365
Joined: 07 Apr 2010, 16:05

06 Mar 2011, 23:06

I guess something like this should work...

Code: Select all

<p:commandButton id="mybutton" ...

Code: Select all

<script type="text/javascript">
                    //<![CDATA[
                    document.getElementById('mybutton').click();
                    //]]>
</script>

User avatar
ydarcin
Posts: 258
Joined: 04 Jan 2009, 19:02
Location: Turkey

07 Mar 2011, 10:02

Hi,

afaik document.getElementById('mybutton').click(); will not work on some browsers so it is better you test it in all browsers,

Yigit

User avatar
Daedalus
Posts: 164
Joined: 28 Jan 2010, 12:03

07 Mar 2011, 11:31

Use

Code: Select all

<script type="text/javascript">
  jQuery('#mybutton').click();
</script>
It will work 100%.
PrimeFaces 3.0, Glassfish 3.1.2, Mojarra 2.1.6,

robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

07 Mar 2011, 13:13

Thanks everybody for your answers, I'll try it out later today and report back what solution worked best for me!

healeyb
Posts: 365
Joined: 07 Apr 2010, 16:05

07 Mar 2011, 15:09

Does anyone know which browsers don't work with getElementById?

User avatar
bumble.bee
Posts: 723
Joined: 29 Sep 2010, 21:39
Location: United States

07 Mar 2011, 15:31

I'm guessing IE (usually a safe bet!) :)

I think IE 6 and 7 getElementById searches both element ID and element Name, which can cause conflicts. There may be other issues...

healeyb
Posts: 365
Joined: 07 Apr 2010, 16:05

07 Mar 2011, 16:40

Thanks, the IE developers must have missed the "ById" suffix then!.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 36 guests