...

View Full Version : javascript command button



GaryJSF
07-11-2012, 10:21 PM
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}"

/>

:confused:

Old Pedant
07-11-2012, 11:01 PM
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??

GaryJSF
07-12-2012, 02:13 PM
Correct. All I needed was the return

GaryJSF
07-13-2012, 11:30 PM
Hi,

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

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;
}

Old Pedant
07-14-2012, 12:07 AM
I have my doubts that you actually have any element in your HTML with the id to match this:


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

alert( prog.value );

If it's an ordinary HTML tag (e.g., <td>,<span>,<div>) then you will use


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.

GaryJSF
07-24-2012, 12:33 AM
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 !!!!!

Old Pedant
07-24-2012, 01:24 AM
Wrong:


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

Right:


<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


var form = document.getElementById("...the form id...");



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum