Passing more than two values in javascript function - javascript

In my jsp page I have a javascript function for a button click.In that I need to pass some values to next jsp page.I can able to pass two values as a parameter but while giving three values its not working.Not working means control is not going next page when click on button.
This one works fine
window.location.assign("gt_Iba2?value="+uri+"&len="+<%=height%>);
This is not working
window.location.assign("gt_Iba2?value="+uri+"&len="+<%=height%>+"&SelectedValue="+<%=typeNameToPass%>);
EDIT
typeNameToPass is a string value i'm getting from previous jsp page.
String typeNameToPass =request.getParameter("value");
My javascript function
<SCRIPT LANGUAGE="JavaScript">
function gt2()
{
var pqr="100";
var arr=new Array();
var x=<%=height%>;
var attstr=null;
for(var t=0;t<x;t++)
{
var a="inputText"+t;
var e=document.getElementById(a);
var val= e.value;
if(val.indexOf(",") !== -1){
alert("Legal value Constraint can't allow comma");
return;
}
arr[t]=val;
if(t==0)
{
attstr=arr[t]+",";
}
if((t!=x)&&(t!=0))
{
if(t==x-1)
{
attstr+=arr[t];
}
else
{
attstr+=arr[t]+",";
}
}
}
var uri=encodeURIComponent(attstr);
window.location.assign("gt_Iba2?value="+uri+"&len="+<%=height%>+"&SelectedValue="+<%=typeNameToPass%>);
I don't know what's wrong here.Any ideas would be much helpful

Try that:
window.location.assign("gt_Iba2?value="+uri+"&len=<%=height%>&SelectedValue=<%=typeNameToPass%>");
Your code works for height, probably because it is number. Then you will get in JS something like "[...]&len="+80, but if typeNameToPass is a string value you will get "[...]&len="+80+"&SelectedValue="+someString - unless someString is a variable, you will get error.

Related

Javascript function parameters in Acrobat

Hopefully you all don't get pissed at me for such a seemingly simple question..
Basically, I have a PDF form that I'm scripting with javascript.
I have a bunch of check boxes that I would like to set required and/or not required based on other inputs and I'm trying to repeat code as little as possible, especially since there's a ton of inputs.
Right now, the best way I can accomplish what I'm attempting is by setting a function for each instance of inputs as follows:
function setWalkwayNotRequired() {
this.getField("sidewalkAsphalt").required = false;
this.getField("sidewalkConcrete").required = false;
this.getField("sidewalkPavers").required = false;
this.getField("sidewalkCondition").required = false;
}
I would then call this function based on the input of a certain checkbox:
if (this.getField("sidewalkNone").value == "Yes") {
setSidewalkNotRequired();
}
Then all of the above-mentioned fields would be set to not required.
I feel like there should be a way to create a single "setRequired" or "setNotRequired" function to take a parameter of the field in question.
In my mind that would look something like this:
function setRequired(a, b, c, d) {
this.getField(a).required = true;
this.getField(b).required = true;
this.getField(c).required = true;
this.getField(d).required = true;
}
I would then call on that function for all instances, for example, walkways (like that above) or driveways, etc. like so:
if (this.getField("sidewalkNone").value == "Off") {
setRequired('"sidewalkAsphalt"', '"sidewalkConcrete"', '"sidewalkPavers"', '"sidewalkCondition"');
}
Again, in my mind what would then be output based on the above code once the function is called is something like:
if (this.getField("sidewalkNone").value == "Off") {
this.getField("sidewalkAsphalt").required = true;
this.getField("sidewalkConcrete").required = true;
this.getField("sidewalkPavers").required = true;
this.getField("sidewalkCondition").required = true;
}
Doing it the way I did in the first code block would require me to create separate functions for each set of checkboxes, creating a lot of code in an already huge file. The second way would allow me to use 1 function over and over throwing the field names as parameters depending on where I'm at in the PDF.
I'm also not very clear on if it's even legal to declare the parameters as I did with the '"..."' quotes; I did that because I need the double quotes inside the this.getField().
Again, I'm sorry if this is novice, I've just been trying to play with the code for a while now and can't get it to work.
Any input would be amazing.
You could just pass in an Array of field names:
function setRequired( fieldNames, isRequired = true ) {
for( var i = 0; i < fieldNames.length; i++ ) {
var fieldName = fieldNames[i];
this.getField( fieldName ).required = isRequired;
}
}
Usage:
if( this.getField("sidewalkNone").value == "Off" ) {
setRequired( [ "sidewalkAsphalt", "sidewalkConcrete", "sidewalkPavers", "sidewalkCondition" ] );
}
If you use hierarchical naming with dot notation, you can set properties on the parent to affect all children. For example, if you name the fields "sidewalk.Asphalt", "sidewalk.Concrete", and "sidewalk.Pavers"...
this.getField("sidewalk").required = true;
... will set all the children to be required.

Concatenation of variable and String wrong

I would like to pass an argument to another page but it turns out that it's undefined.
getAccount() returns a list of json. Firstly, I display those json objects one by one on HTML and when the user clicks each, the accDetail[i].accNo is set as local storage and will be passed to next page.
var accDetail=getAccounts();
for(var i =0;i<accDetail.length;i++) {
document.getElementById("demo").innerHTML+=''+accDetail[i].accNo +''+' '+ accDetail[i].accType+' '+ accDetail[i].balance+'<br>';
}
This is the function to set the item as local storage.
function getAcc(item)
{
localStorage.setItem("accNo",item); }
It does not display the value I want, is the way I concatenate it wrong?
Your onclick handler is going to be literally getAcc(accDetail[i].accNo) (try viewing in Inspect Element), which won't work because accDetail is not defined in the event handler. You need to change your Javascript so that it writes getAcc(0), getAcc(1), etc where 0, 1 are the different accNo.
Here is a small example I wrote, hopefully you can extend it to solve your problem:
var accNo = [1, 2, 3, 4];
for(var i = 0;i < accNo.length; i++) {
var line = '' + accNo[i] + ' <br>';
console.log(line);
document.getElementById("demo").innerHTML += line;
}
First Make sure your function getting correct value
Try Using window object instead
function getAcc(item)
{
localStorage.setItem("accNo",item);
}
Replace with
function getAcc(item)
{
console.log(item); //Please check you get item correct not undefined
window.accNo = item;
}
And whereever you want just use window.accNo

Adding control ID's into Global Array variable

I am pretty sure I understand why this is not working. I think its because at the time the controls are trying to be found, the page has not rendered them yet.
Assuming that is the case, My question is how can I set these Arrays after the page has been fully loaded and still access them globally?
If I set the Arrays in the function my code works but it seems like a waste to find them like that as there will end up being 12 controls per array and the function really only needs to access 2 controls.
Here is some of my code:
//The controls in the following arrarys can not be found at this point of execution.
//If I put these Arrays into my function , the code works and the controls are found.
var RadRatingArray = [$find("<%= RadRating_Rating0.ClientID %>"), $find("<%= RadRating_Rating1.ClientID %>")];
var RadRatingNAArray = [$find("<%= RadRating_NA0.ClientID %>"), $find("<%= RadRating_NA1.ClientID %>")];
var RadTextBoxArray = [$find("<%= RadTextBox_Comment0.ClientID %>"), $find("<%= RadTextBox_Comment1.ClientID %>")]
//This function is called when the NA rating control is clicked. It clears the rating control and sets a number of other elemnts on the page.
function ClearRadRating_Rating(sender, args) {
var Number = sender.get_id().slice(-1);
var IntNum = parseInt(Number, 10);
if (sender.get_value() !== 0) {
RadRatingArray[IntNum].set_value(0);
}
SetSelectedRating(sender, RadTextBoxArray[IntNum], "Message" + [Number], "CallOut" + [Number]);
}
Thanks!

What does unescape() function returns?

I have to use unescape() function in an if-else statement. In my website I have two pages, one with a form that the user fills and the second page have to get the information from the filled form by unescape function. I need the if-else statement because in the form I put two radio buttons that each one adds different text areas with different ids' and names so I want to check in the second page what text fields are are filled. (all the fields created by clicking on a radio button which starts a javascript function so in the next page I must check if the field was created and not just to check if it is an unfilled text field).
It is a little bit hard for me to explain so just check the code. In the code you will see params["placeName"] and so on, so placeName for example like all the others is a text field name from the previous page.
So the question is - what does unescape function returns if the component name I insert as a paramater does exist in the previous page?
<script type="text/javascript">
function getParams() {
var idx = document.URL.indexOf('?');
var params = new Array();
if (idx != -1) {
var pairs = document.URL.substring(idx + 1, document.URL.length).split('&');
for (var i = 0; i < pairs.length; i++) {
nameVal = pairs[i].split('=');
params[nameVal[0]] = nameVal[1];
}
}
return params;
}
params = getParams();
//from here it is what I want to do (I don't know if this condition in the if statement is correct, this is what I ask)
// if (unescape(params["placeName"]) == false) {
// }
// else {
var place = unescape(params["placeName"]);
var country = unescape(params["country"]);
var city = unescape(params["city"]);
var address = unescape(params["address"]);
var type = unescape(params["type"]);
var rate = unescape(params["rate"]);
// }
</script>
It can also work if I could check what radio button is checked
You are asking what will unescape(params["something"]) return if params["something"] is not present. The answer is undefined. So you need to check equivalence to "undefined". Meaning: if (unescape(params["placeName"]) == "undefined") (in this case params["placeName"] is not present (was not created).
The undecode function returns -as it's name indicated- a decoded string. If you enter a value that's not defined it will probably cause an error because of the indefinition. If what you want is to check whether the element was or not created or not you could use
if (params.placeName !== undefined) {
// It was created
}
instead.
Aditionally, if you want to check which radio button was checked use
if (document.getElementById('foo').checked) {
// Radio button with id 'foo' was checked
} else if (document.getElementById('bar').checked) {
// Radio button with id 'bar' was checked
} else if
...

How do I correctly use an iteration value in JavaScript?

I am creating a 'simple' javaScript function which basically displays new information on the page when a user clicks next or previous.
The information is taken from an array and I want to use either i++ or i-- to call an element of the array.
Heres my JavaScript:
var titles = ["Dundalk", "Navan", "Drogheda", "Dublin"];
var i = 0;
function next()
{
i++;
if (i == titles.length)
{
i = 0;
}
var object = document.getElementById('tname');
object.innerHTML = titles[i];
}
function prev()
{
if (i == 0)
{
i = titles.length;
}
i--;
var object = document.getElementById('tname');
object.innerHTML = titles[i];
}
The problem is, when I run this code in my HTML page, I get an 'UNDEFINED' result. The JavaScript is not recognizing that i has been initialized as 0 in the beginning.
If i change titles[i] to titles[2], for example, the correct text is displayed in HTML.
What am I forgetting or how can I overcome this?
Thanks
The fact that you're seeing undefined indicates that you're accessing an array index which hasn't been set. Your code looks fine at a glance, so I would guess that there's some more code you're not showing which also uses i as a loop variable and leaves it set to a value > titles.length after the code above has run.

Categories