954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Js string creation from an array

Hello,

1. I have the following script:

function selectThis(id){
var locationstring = "process.php?value=" + id; /////
locationstring += "&bs=" + $('#bs_' + id).attr("selectedIndex");
locationstring += "&ad=" + $('#ad_' + id).attr("selectedIndex");
locationstring += "&ns=" + $('#ns_' + id).attr("selectedIndex");
locationstring += "&wt=" + $('#wt_' + id).attr("selectedIndex");
locationstring += "&in=" + $('#in_' + id).attr("selectedIndex");
document.location.href = locationstring;
}

With it I am getting a string with the value of the clicked button from an array and some other select field values that are listed in the same set of mysql results as the clicked button.

BUTTON:

At the end of the script the page is redirected to in the script mentioned page with following string:

process.php?value=1&bs=2&ad=3&ns=4&wt=5&in=6

(values are not real, so whatever the chosen value of the select field should apear in the link.)

THIS WORKS FINE.


2. My problem is following:

In order to be able to use var locationstring in other scripts as well I have removed var and have made it global.

function selectThis(id){
locationstring = "process.php?value=" + id; /////
locationstring += "&bs=" + $('#bs_' + id).attr("selectedIndex");
locationstring += "&ad=" + $('#ad_' + id).attr("selectedIndex");
locationstring += "&ns=" + $('#ns_' + id).attr("selectedIndex");
locationstring += "&wt=" + $('#wt_' + id).attr("selectedIndex");
locationstring += "&in=" + $('#in_' + id).attr("selectedIndex");
}

The problem is that later on when I use locationstring variable in other script I get only first variable in the string

process.php?value=1

instead of

process.php?value=1&bs=2&ad=3&ns=4&wt=5&in=6

3. Is it possible to make locationstring variable global and in a single line in order to be able to use the full string in other scripts or is there another solution.

Thank in advance.

Tchibo
Newbie Poster
2 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

I got it:

function selectThis(id){
var bs=document.getElementById("bs").selectedIndex;
var ad=document.getElementById("ad_" + id).selectedIndex;
var ns=document.getElementById("ns_" + id).selectedIndex;
var wt=document.getElementById("wt_" + id).selectedIndex;
var ins=document.getElementById("in_" + id).selectedIndex;
locationstring = "?book=" + id + "&bs=" + bs + "&ad=" + ad + "&ns=" + ns + "&wt=" + wt + "&in=" + ins;
}

Tchibo
Newbie Poster
2 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Declaring variables with the keyword "var" outside of functions, makes a global var anyway;
But declaring it with 'var' keyword inside a body function makes it a local var.
therefore for internally initialized variables to be globally accessible you simply drop the variable constructor declaration.
This might also do just fine:
function selectThis(id){
locationstring = "process.php?value=" + id+
"&bs=" + $('#bs_' + id).attr("selectedIndex")+
"&ad=" + $('#ad_' + id).attr("selectedIndex")+
"&ns=" + $('#ns_' + id).attr("selectedIndex")+
"&wt=" + $('#wt_' + id).attr("selectedIndex")+
"&in=" + $('#in_' + id).attr("selectedIndex")+
}

Troy III
Practically a Master Poster
609 posts since Jun 2008
Reputation Points: 120
Solved Threads: 80
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: