0

var name = $(".column_nr_matching:last").attr("name");
// this gives me col_nr359 and here I add plus 1

var added_one = name + (+1);

When I look with console it gives me this:

col_nr3591

I need it to be

col_nr360
LOLA
  • 57
  • 7
  • The questions is not clear – Hussein Salman Mar 12 '18 at 19:18
  • 1
    What happens when you try to add a number to a string? It concatenates. You need to find a solution to get a number from your string first, add to that number, then back to string. –  Mar 12 '18 at 19:18
  • Well, `col_nr359` is a string, not a number, so adding a `1` to it causes Javascript to convert that `1` to a character and append it as a string. Even if you had `'123' + 2` you'd get `'1232'` since `'123'` is a string. You'll need to split out the number, add the 1 as number added to a number, and reconstitute the string. – lurker Mar 12 '18 at 19:19
  • I want to add + 1 but it does not add it just puts the number 1 to the end instead of increasing – LOLA Mar 12 '18 at 19:19
  • Yes, that is correct. And two of us have given you the reason why that's happening. Do you understand the reason? As I said, you need to do some research on javascript string handling utilities, split the number out of the string as a stand alone number (not a substring), add the 1 to it as a number, then reconstruct the string. – lurker Mar 12 '18 at 19:20
  • I think you should provide more code – mejiajuanbta Mar 12 '18 at 19:25

4 Answers4

4

Because col_nr359 is a string, and by using +, you are concatenating 1 to that string. You need to do something like this:

document.getElementById('increment').onclick = function (){
  //get the innertext from the span
  var text = document.getElementById('colid').innerText;
  //replace the 'col_nr' text with empty string to get the number
  var nr = text.replace('col_nr','');
  //parses the number to int and sums 1, then concatenate back
  var new_id = 'col_nr' + (parseInt(nr) + 1);
  //set the new text to the span
  document.getElementById('colid').innerText = new_id;
  
}
<span id="colid">col_nr359</span>
<br>
<button id="increment" > Increment </button>
Artur Trapp
  • 434
  • 3
  • 13
1

As mentioned, name is a string, not a number. You need to just get the number part so you can add one to it. One possible way of doing so is with a regex string replace, where you use capture group to get the number part, and replace it with that number plus one. Example:

const name = "col_nr359";
const added_one = name.replace(/(\d*)$/, m => String(+m + 1))
console.log(added_one)
CRice
  • 29,968
  • 4
  • 57
  • 70
0

col_nr359 is a String, even JavaScript can't interpret it as number, since there are characters in front.

What you have to do is split the string so you have col_nr and 359, then add 1 to the 359, then concatenate it again.

David Walschots
  • 12,279
  • 5
  • 36
  • 59
-1

You are mixing datatypes. col_nrxxx is a string and will always be one. You have to cut out the 359, then parse it to int, then add the +1, then concat it back to the string.

evayly
  • 832
  • 7
  • 18