Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    javascript command button

    Hi,

    I am trying to get a javascript funtion to return false to my commandbutton so if the user selects cancel the action will not execute. I know I can put the command directly in the button and this works but I have other code I want to add the the function but for now I am only trying to get this basic thing working. Thank You.

    <script type="text/javascript">
    function confirmTacticalDesign() {
    if (!confirm("Warning - Do not pull corporate numbers for Patriot Tactical Design!")) {
    return false;
    }
    }
    </script>

    <h:commandButton value="Submit"
    id="test"
    onclick= "confirmTacticalDesign()"
    action="#{requestAddEdit.InsertRequest}"

    />


  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,986
    Thanks
    79
    Thanked 4,432 Times in 4,397 Posts
    onclick="return confirmTacticalDesign()"

    I think that's all you are missing. Dunno what a "command button" is. I'm assuming you are using some Java or ASP.NET framework??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    GaryJSF (07-12-2012)

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Correct. All I needed was the return

  • #4
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi,

    Making some progress but the alert returns: Test False [object].
    How to I get it to return a value?

    THANK YOU!!!

    javascript
    <script type="text/javascript">
    function confirmTacticalDesign() {

    var prog = document.getElementById("form1:inputHiddenReqId");
    if (!confirm("Warning ")) {
    alert ('Test false ' + prog);
    return false;
    }
    else
    alert ('Test true ' + prog);
    return true;
    }
    </script>
    </head>


    jsp (jsf) :
    <h:inputHidden binding="#{requestAddEdit.inputHiddenReqId}"
    id="inputHiddenReqId"
    value="#{requestAddEdit.requests.reqId}"/>


    backing bean:
    public void setInputHiddenReqId(HtmlInputHidden inputHidden1) {
    this.inputHiddenReqId = inputHidden1;
    }

    public HtmlInputHidden getInputHiddenReqId() {
    return inputHiddenReqId;
    }

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,986
    Thanks
    79
    Thanked 4,432 Times in 4,397 Posts
    I have my doubts that you actually have any element in your HTML with the id to match this:
    Code:
    var prog = document.getElementById("form1:inputHiddenReqId");
    To find out:

    Bring up the page in your browser.
    Click on the browser's VIEW menu.
    Click on the view's SOURCE or PAGE SOURCE menu item.

    Now you are looking at the HTML that your JSP page is generating.

    Find the field in question.

    I would bet that it's id really is just what you specified: "inputHiddenReqId". Why would the JSP code insert "form:" into the ID??

    *** BUT ***

    Assuming I am wrong, then you STILL need to know what kind of object the getElementById found. If it is any kind of <form> field (e.g. <input>, <select>, <textarea>) then you will want to use
    Code:
         alert( prog.value );
    If it's an ordinary HTML tag (e.g., <td>,<span>,<div>) then you will use
    Code:
        alert( prog.innerHTML );
    In any case, learn to look at the HTML generated by the JSP framework using VIEW>>SOURCE and, better, learn to use a JavaScript debugger.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    GaryJSF (07-23-2012)

  • #6
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    javascript command button

    The prog.value worked but I am having trouble returning the value of a selectOneRadio. It is returning undefined in the alert

    jsf code:
    <h:selectOneRadio value="#{requestAddEdit.requests.deliveryType}"
    binding="#{requestAddEdit.selectDelType}"
    id="selectDelType">
    <f:selectItem itemLabel="A-Government Number" itemValue="A"/>
    <f:selectItem itemLabel="B-Corporate Number" itemValue="B"/>
    </h:selectOneRadio>


    java script:
    <script type="text/javascript">
    function confirmTacticalDesign() {
    var delt = document.getElementById("form1:selectDelType").value;
    alert ('del type is: ' + delt);
    </script>


    view source:
    <td><table id="form1:selectDelType">
    <tr>
    <td>
    <input type="radio" name="form1:selectDelType" id="form1:selectDelType:0" value="A" /><label for="form1:selectDelType:0"> A-Government Number</label></td>
    <td>
    <input type="radio" checked="checked" name="form1:selectDelType" id="form1:selectDelType:1" value="B" /><label for="form1:selectDelType:1"> B-Corporate Number</label></td>
    </tr>
    </table></td>



    Thank you for your help !!!!!

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,986
    Thanks
    79
    Thanked 4,432 Times in 4,397 Posts
    Wrong:
    Code:
    <script type="text/javascript">
    function confirmTacticalDesign() {
    var delt = document.getElementById("form1:selectDelType").value;
    alert ('del type is: ' + delt);
    </script>
    Right:
    Code:
    <script type="text/javascript">
    function confirmTacticalDesign() {
         // find the <form> that holds one of the radio buttons
         // (if you know the id of the <form>, do it directly instead of like this, but this works)
         var form = document.getElementById("form1:selectDelType:0").form;
    
         // radio buttons are an *ARRAY* in JavaScript, so...
         var rbs = form["form1:selectDelType"]; // get the array
         for ( var r = 0; r < rbs.length; ++r )
         {
             if ( rbs[r].checked ) 
             {
                 // the one that is checked is the value of the set
                 // what do you want to do with that?
                 // I'm assuming you want to RETURN the checked value from this function:
                 return rbs[r] .value;
             }
         }
         // if we get here, neither one was checked
         // what do you want to do ?
         // give a message?
         alert( "You did not check either corporate number");
         // maybe return null, to indicate none were checked?
         return null;
     }
    </script>
    If the VIEW==>>SOURCE shows that your <form>has an ID, just replace the first line there with
    Code:
        var form = document.getElementById("...the form id...");
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •