Advertisement
Koragg

JavaScript Numbers Addition Fix [JavaScript]

Dec 16th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <input id= "test1" value="test1" onchange="test(this.id)">
  2. <input id= "test2" value="test2" onchange="test(this.id)">
  3. <input id= "test3" value="test3" onchange="test(this.id)">
  4. <!-- https://stackoverflow.com/questions/8976627/how-to-add-two-strings-as-if-they-were-numbers -->
  5. <!-- parseInt and numbers addition possible fix below -->
  6. <!-- +id + +1 -->
  7. <script>
  8.     function test(string) {
  9.         var id = string.replace("test", "");
  10.         alert('curid' + parseInt(id));
  11.         alert('previd' + parseInt(id-1));
  12.         alert('nextid' + parseInt(+id + +1));
  13.         var curElement = document.getElementById("test" + parseInt(id));
  14.         var prevElement = document.getElementById("test" + parseInt(id-1));
  15.         var nextElement = document.getElementById("test" + parseInt(+id + +1));
  16.         if (curElement) {
  17.             alert('current element: ' + curElement.value);
  18.         }
  19.         if (prevElement) {
  20.             alert('previous element: ' + prevElement.value);
  21.         }
  22.         if (nextElement) {
  23.             alert('next element: ' + nextElement.value);
  24.         }
  25.     }
  26. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement