Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split an array in JS 1

Status
Not open for further replies.

A1Pat

IS-IT--Management
Jun 7, 2004
454
US
How can I translate this ASP code to JS code
Code:
	<% 
						arrChannels = Split(Session("cam_no"), ",")
						For Each strOneWord in arrChannels
	response.write strOneWord & "<BR>"
 	
						Next
	%>
To simply explain the ASP code on the above, all it does is to take a string (ie: 1,2,3,4,5) and break it down then print it one by one in a single line such as:
1
2
3
4
5
and so on.

Thanks!
 
google

[sub]____________ signature below ______________
You may get help by means of code here. Just recall what happens to a rat when he eats the cheese off a rat trap while you're Ctrl+C/Ctrl+V'ing that code as your own[/sub]
 
Hi

Then you do not split an array. You split a string to an array.
Code:
<script type="text/javascript">
arrChannels=[i]'whatever'[/i].split([i]','[/i]);
[b]for[/b] (i=0;i<arrChannels.length;i++) {
  document.write(arrChannels[i]+[i]'<br>'[/i]);
}
</script>
Of course, you can not access session objects on client-side.

Feherke.
 
Do something like this to get cam_no into a client side array, I have to all the time:

ASP
Code:
var channelsString = Session("cam_no")

Javascript
Code:
var channelsArray = "<%=channelsString%>".split(",");


[monkey][snake] <.
 
Since the cam_no is getting from a field in a form, I'm going to stick that string into the field and send it to the js code to do the split like Feherke suggested.

Code:
<script type="text/javascript">
cam_no = Channel.value;
arrChannels='cam_no'.split(',');
for (i=0;i<arrChannels.length;i++) {
  document.write(arrChannels[i]+'<br>');
}
</script>

Lets find out, shall we!
 
by the way, I hope u don't mind I'm doing a short cut here asking u guy a easy question that I can obtain from doing a little search elsewhere...

how can I do a if...else to validate whether the cam_no is a string with "," or not. I use to do this validation for email address

Code:
if (document.contactUs.email.value.indexOf("@") == -1 || document.contactUs.email.value.indexOf(".") == -1)

but I don't think I want to use the same thing for this particular case. In another word, I want to say "if the string cam_no has (,) then... do the split..."

thanks!
 
Hi guys,

why this Split doesn't have a result like the ASP one I had on the above, I test the output from this code

Code:
		var arrChannels=m_chNum.split(",");
		for (i=0;i<arrChannels.length;i++) {
		  document.write(arrChannels);
		}

and here is the result
1,21,2

 
Thanks Feherke for the info!
 
If Feherke of someone can help fix up this code a little 'cause I just find out this code has a small flaw

Code:
<script type="text/javascript">
cam_no = Channel.value;
arrChannels='cam_no'.split(',');
for (i=0;i<arrChannels.length;i++) {
  document.write(arrChannels[i]+'<br>');
}
</script>

and the flaw is... what if the Channel.value is not 1,2,3... but the value might be 1,12,15,etc. then the document.write(arrChannels) will then not neccessary reflect each of then Channel.value in the original string, won't it?

So, my goal is now find a way to display each of the value in the Channel string instead of the of the For loop.
 
Nevermind... Thank you for NOT reply this stupid question.

I either must be high or stupid to realize this line

document.write(arrChannels+'<br>');

... Sorry anyone if you had to view the question.

:)
 
Don't be so hard on yourself A1Pat. If I got paid for each time I chased something like that I could have retired DateTime end = DateTime.Now.AddYears(-10);

:)

[sub]____________ signature below ______________
You are a amateur developer until you realize all your code sucks.
Jeff Atwood[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top