locked
Trying to get SUBSTRING RRS feed

  • Question

  • User-1540820643 posted

    I have a label control with text  Present: 1/1(2%)

    I want to change via script to Present 1(2%)

    When testing, I can get the string Present: 1 but I can't get the other half (2%) . What am I missing? 

    <script>
        //var x = document.getElementById("Label1");
        //document.getElementById("Label1").innerHTML = x.innerHTML.substring(0, 6);
    
    
    
    
    
        var y = document.getElementById("Label2");
        var nStart = y.innerHTML.indexOf("/")
        var nEnd = y.innerHTML.indexOf("(")
        document.getElementById("Label2").innerHTML = y.innerHTML.substring(0, nStart); 
        document.getElementById("Label2").innerHTML = y.innerHTML.substring(nEnd,5);
       document.getElementById("Label2").innerHTML = y.innerHTML.substring(0, nStart) + y.innerHTML.substring(nEnd,5)
    </script>

    document.getElementById("Label2").innerHTML = y.innerHTML.substring(0, nStart);   <--- Gives me Present: 1   

    document.getElementById("Label2").innerHTML = y.innerHTML.substring(nEnd,5);  <--- Gives me nt: 1/1

    document.getElementById("Label2").innerHTML = y.innerHTML.substring(0, nStart) + y.innerHTML.substring(nEnd,5) <-- Gives me Present: 1nt: 1/1

    Thursday, January 28, 2021 3:52 PM

Answers

  • User-474980206 posted

    in .substring(start,end) the parameters are the indexes. so in .substring(nEnd, 5) is the same as .substring(12, 5).  because 12 is greater than 5, the args are switched, and become .substring(5,12). "Present 1/1(2%)".substring(5,12) returns the characters between position 5 and 12 or "nt: 1/1"

    not sure why you use such complex code when a simple regEx would work:

        document.getElementById("Label2").innerHTML = document.getElementById("Label2").innerHTML.replace(/\/\d*/,"");
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 28, 2021 4:42 PM

All replies

  • User-474980206 posted

    in .substring(start,end) the parameters are the indexes. so in .substring(nEnd, 5) is the same as .substring(12, 5).  because 12 is greater than 5, the args are switched, and become .substring(5,12). "Present 1/1(2%)".substring(5,12) returns the characters between position 5 and 12 or "nt: 1/1"

    not sure why you use such complex code when a simple regEx would work:

        document.getElementById("Label2").innerHTML = document.getElementById("Label2").innerHTML.replace(/\/\d*/,"");
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 28, 2021 4:42 PM
  • User-1540820643 posted

    Thank you.

    Wednesday, February 3, 2021 8:52 PM