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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NaN when it should be a string?

Status
Not open for further replies.

unktehi

Programmer
Sep 13, 2006
5
US
This is the page I'm working on:

Ideally when a complicated link such as:

is pasted into the form and the button is pushed, I'd like strip out uneeded parts, however, for some reason, when I split it, where it should display the array of parts, it comes up with NaN. Any idea why - when it should be displaying the strings?
 
There are two minor problems in your code.
First, you need to declare the variable html outside of your loop otherwise you are re-declaring it every iteration of the loop. Also, you need to assign it a default value since you cannot use += on something that has no value to begin with.

The second issue is just that you used =+ instead of +=.
The code below should work for you.

Code:
[COLOR=red]var html = '';[/color]
for(i=0; i<val1.length; i++) {
	html [COLOR=red]+=[/color] val1[i];
	//alert(html);
		 }

At my age I still learn something new every day, but I forget two others.
 
I've made those small changes in the code, but now it doesn't do anything. I'm getting the error the cleanLink() is not defined - but I think this error is misleading, because I didn't change anything relating to that.

 
Change this:
Code:
[COLOR=red]var [/color]html += val1[i];
to this:
Code:
var html += val1[i];

With the var in front of it you are redefining the variable every iteration through the loop.
It is also causing an error when the page loads so your script does not run.

At my age I still learn something new every day, but I forget two others.
 
I think niteowl meant to remove the var from his 2nd code box.

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
You haven't removed the var on the website as of this posting. You can't initialize a variable using +=, which is what your code does.

Lee
 
Looking at your code (rather than the Firefox JS console), you mistakenly removed the var from the initialization on line 72, not from line 74.

Lee
 
I change that and took the vars out in both places. I re-uploaded it in and it works like it did before - going through each string separately - but not listing them all out.
 
You really need to pay attention yourself to the code you've written rather than expect everyone here to debug every detail. This:

Code:
Parts.innerHTML ="" + val1[i] + "<br>";

should be

Code:
Parts.innerHTML [b][red]+[/red][/b]= val1[i] + "<br>";

Lee
 
unktehi said:
I change that and took the vars out in both places. I re-uploaded it in and it works like it did before - going through each string separately - but not listing them all out.

You have to initialize the variable using var so the line OUTSIDE of the loop should be: var html = '';

Inside the loop you need:
html += val1;
If you have var in front of that line you are re-initializing the variable which would set it to no value then trying to use += on a variable with a null value which will cause you an error.

Partly this is my fault. In my post above I said "Replace this... With this...." and I neglected to remove the word var in the "With this...." portion. I was cutting and pasting and just forgot to exempt that piece so it was confusing.


At my age I still learn something new every day, but I forget two others.
 
trollacious - I would love to - but I don't know javascript well enough yet to know exactly what to look for with some errors.

Thank you both I got it to work!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top