/    Sign up×
Community /Pin to ProfileBookmark

Passing a variable to an array

I’m trying to pass a variable to a function to create an array via an onclick event. Everything works except for this line “arrSortBy = new Array(b);”. It works if I manually plug in the correct values (i.e. arrSortBy = new Array(“One”,”Two”,”Three”); ), but the values change from page to page so I need to pass a variable instead of having static code. Can anyone tell me what I’m doing wrong? Here’s my code.
Javascript

[CODE]
function cmdClearSortBy(a){
var b = a.replace(/,/g, “”,””);
b = “”” + b + “””;
arrSortBy = new Array(b);
var m;
var n;
for(i=0;i<arrSortBy.length;i++){
m = arrSortBy[i] + “ASC”;
n = arrSortBy[i] + “DESC”;
document.getElementById(m).src = “../Images/down_arrow_grey.gif”;
document.getElementById(n).src = “../Images/up_arrow_grey.gif”;
}
var x = document.forms.PURLHits;
x.txtSortBy.value = “”;
}
[/CODE]

HTML

[CODE]
<input type=”button” name=”ClearSortBy” value=”Clear” onclick=”cmdClearSortBy(‘One,Two,Three’)” tabindex=”7″>
[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMay 21.2007 — Try passing the array instead of the string version of the array.

See example here.
[code=php]
<html>
<head>
<title>Array Sort</title>
<script type="text/javascript">
function sortArray(arr) {
return arr.sort();
// note sort can be changed for numeric sorts as well
// or reverse order sorts or case-insensitive sorts, etc.
}
function sortTArea() {
var A_Arr = new Array();
var tmp = document.getElementById('TArea').value;
tmp = tmp.replace(/n/g,' '); // incase user used RETURN as seperator
// could also check for ',' separator or '|' or '?', etc.
tmp = tmp.replace(/s+/g,' '); // remove multiple space separators
A_Arr = tmp.split(' ');
var B_Arr = new Array();
B_Arr = sortArray(A_Arr); // --- HERE IS CRUX OF EXAMPLE ---
var msg = ''
for (var i=0; i<B_Arr.length; i++) { msg += B_Arr[i]+"n"; }
document.getElementById('sortedTArea').value = msg;
}
</script>
</head>
<body>
<textarea id="TArea" rows="20">z y x c b a</textarea>
<button onClick="sortTArea()">Sort</button>
<textarea id="sortedTArea" rows="20"></textarea>
</body>
</html>
[/code]

Note comment in middle of 'sortTArea()' example setup.

Does not do much, but does show concept.

Modify as needed for your application.
×

Success!

Help @nbcrockett spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 10.12,
social: @webDeveloperHQ,
});

legal: ({
terms: of use,
privacy: policy
analytics: Fullres
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @aldoushuxley,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: @linalla,
tipped: article
amount: 1000 SATS,
)...