3

Given the circumstances I have to use a node list to get at elements that I need to work with. I am able to use .textContent (or .nodeValue) to return a string that I need, but I am having trouble finding a way to get that string into any type of number. I need to have it in number form so I can perform calculations with it. I have tried Number(), parseInt(), etc. All return NaN. I am pretty new to JS so hopefully this is an easy issue to solve.

var siteActual = tdNodeListActual[36].textContent; // location of data
console.log(siteActual); // returns the value 1 as it should

var asd = Number(siteActual); // test variable to convert string to number
console.log(asd); // returns NaN

EDIT: I checked .length as one of you suggested and it was 2, so those saying it may be an invisible character are probably right. I am trying to make changes to a SharePoint page, that is why I am not familiar with what the markup is. Any suggestions on how to remove all but the number would be helpful.

4
  • 2
    try parseInt(String) Commented Jul 14, 2015 at 19:16
  • 3
    What's in siteActual? do you have an example? Number("1") works, that's why I'm asking again. Commented Jul 14, 2015 at 19:17
  • 1
    I guess another question is: how have you used parseInt(...) - on which element? I suppose more details would be necessary (i.e. some HTML sample on where you are getting the value from to begin with) Commented Jul 14, 2015 at 19:22
  • 1
    try logging siteActual.length and see if it's what you expect Commented Jul 14, 2015 at 20:04

2 Answers 2

2

Your code should work as-is if the content is really just 1 (as should parseInt). So there must be something other than valid numbers in your content.

Sometimes there can be content that you can't see, e.g. in the example below the second td contains an invisible "zero-width space" character.

var tdNodeListActual = document.querySelectorAll("td");

var good = tdNodeListActual[0].textContent;
var bad = tdNodeListActual[1].textContent;

alert(good + "==" + Number(good) + ", but " + bad + "==" + Number(bad))
<table><tr>
  <td>1</td>
  <td>&#8203;2</td>
</tr></table>

You can remove all non-digit characters (except . and ,) using a regex, e.g.:

siteActual = siteActual.replace(/[^\d.,]/g, '');
Sign up to request clarification or add additional context in comments.

6 Comments

I checked .length as one of you suggested and it was 2, so those saying it may be an invisible character are probably right. I am trying to make changes to a SharePoint page, that is why I am not familiar with what the markup is. Any suggestions on how to remove all but the number would be helpful.
Yes, you can do it with regex - added to answer
Welcome. I've edited your comment from above into the question as I think it helps clarify - hope that's ok.
Sorry to bring this back up from its resting place, but I have an additional question regarding what you've helped me with already. The regex you provided works good, but it is possible for some values to have a decimal point in them. How would I go about removing the blank space, but retaining a decimal point if one exists?
add the dot inside the [] - I added . and , above, in case you need to deal with international number formats
|
2

Using parseInt(...)

Sample:

var siteActual = tdNodeListActual[36].textContent; // location of data
console.log(siteActual); // returns the value 1 as it should

var asd = Number(parseInt(siteActual)); // test variable to convert string to number
console.log(asd); // should return 1

3 Comments

What purpose does parseInt serve? If parseInt works, then Number works - also you should provide a radix on parseInt..
For what he wrote in the question, he already tried parseInt()
@MazzCris true, I didn't read that far down before I answered... asked again for more information in follow-up comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.