0

I am trying to parse this array of checkbox values and remove the "N".

Using the replace function yields the error TypeError: numberArr[i].replace is not a function, and if I remove the replace function, the script simply outputs NaN for my values.

Fiddle here http://jsfiddle.net/j0w5tkg9/3/

<div id='opts'>
    <input type="checkbox" data-cost="0" value="44N12345" name="options[]">
    <input type="checkbox" data-cost="0" value="55N6789" name="options[]">
</div>
<div id='output'></div>


 function getAllocArray() {
     var numberArr = $.map($('input:checkbox:checked'), function (e, i) {
         return +e.value;
     });
     var varOutput = '<ul>';
     for (var i = 0, len = numberArr.length; i < len; i++) {
         varOutput += "<li>" + numberArr[i].replace(/N/,'') + "</li>";
     }
     varOutput += '</ul>';
     $('#output').text(varOutput);
 };

 $('#opts').on('click', 'input:checkbox', null, getAllocArray);
4
  • In our case you can use parseInt(+e.value) instead of +e.value. parseInt('44N12345') returns 44, but Number('44N12345') returns NaN because there is string N Commented Jan 25, 2015 at 16:23
  • The attempt to convert something like "44N12345" to a number by prefixing it with a + operator is what's giving you NaN. Commented Jan 25, 2015 at 16:24
  • And why not return the LI elements in the mapping instead of iterating over the same thing twice ? Commented Jan 25, 2015 at 16:26
  • jsfiddle.net/j0w5tkg9/4 Commented Jan 25, 2015 at 16:32

1 Answer 1

2

Do not convert value to Number. Replace +e.value to e.value:

 var numberArr = $.map($('input:checkbox:checked'), function (e, i) {
     return e.value;
 });

Numbers does not have replace method.

Sign up to request clarification or add additional context in comments.

1 Comment

I feel so stupid right now ! ;-o Just need to wait the mandated 11 minutes to accept your super-quick answer ! THANK YOU !

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.