commandButton 'onclick' doesn't execute javascript method in XHTML page


 if you can remove the parts of samples that don't actually apply to your problem, it makes it easier for us to see what's wrong.

Pedantry: JSF backing beans are not Controllers. You don't write Controllers in JSF - they are pre-written parts of the JSF tag implementations. Except for the FacesServlet, which is the master controller, and is also something that you almost always use "as is". Backing beans are Models. The action code isn't Controller code, it's business logic independent of the MVC paradigm.

And finally, the answer. And you're going to kick yourself.

The reason the "onclick" won't work in your example is because your onclick attribute calls "setTheValue()". But the JavaScript you defined is named "setValue()".

Well, it's not obvious why the javascript function isn't working (Q: have you tried making it ONLY call "alert"? without the other stuff?). However, the onclick attribute itself needs to return true or false, because onclick not only attaches functionality on the client side, it also checks to see if that functionality should suppress submitting the form to the server.

Your suggestion about giving the form an id and checking the 'Page Source' helped me resolve this issue.
When I make the call to getElementById using the form id with the element id it works like a charm.
Like this

            <script type="text/javascript" >  
                var iid1 = "";  
                function setTheValue() {  
                    iid1 = "value";  
                    alert(iid1);  
                    var depelement = document.getElementById('assetsform:deprecation');  
                    var depvalue=depelement.value;  
                    //var depvalue=document.getElementsByName('deprecation').value;  
                    alert(depvalue);  
                    //var cost = getCost();  
                    var costId = 'assetsform:cost';  
                    var cost = document.getElementById(costId).value;  
 
                    if (cost == null) return;  
                    if (depvalue > 0 ) {  
                        var newval = cost-depvalue;  
                        alert(newval);  
                        var val = document.getElementById('assetsform:tvalue');  
                        val.value = newval;  
                        //cost.disabled = true;  
                    }  
                }  
           
            </script> 

Back  [1] [2] 

Copyright © 2007-2012 www.chuibin.com Chuibin Copyright