Completing the React Function of a Common Browser Menu
In this eighth part of a ten-part series on building a common browser menu, we treat the last code segment of the react() function. This is the last part of the in-depth analysis we are making of the react() function.
Completing the React Function of a Common Browser Menu - Explanation of the Seventh Code Segment Continued (Page 3 of 4 )
The next statement we have is an if-statement. The rest of the statements in this code segment are in this if-block. We have to know whether or not the value of the otherSpanNo variable is for a SPAN element that has to be displayed. At each iteration, we want the number for the SPAN element that does not have to be displayed.
When you have a sub menu, in order to have another sub menu, your mouse pointer has to move to some ancestor menu item. This means that the number of digits in a SPAN number whose element you want to remove will always be greater than the number of digits of the SPAN number for the SPAN element in the current cell. The condition of the if-statement here tests for this. If the result of the test is true, then we can proceed to remove the SPAN element at that iteration. In other words if the result is true, the statements in the if-block will be executed. However, before we remove the SPAN elements, there are a few other things to consider.
If a SPAN element of a sub menu has to be displayed, then the digits that its SPAN numbers begin with have to be the same as the digits of the SPAN number (spanNo) of the current cell, in the same order. If this is not the case for any cell in the iteration, then any such SPAN element displayed has to be removed (un-displayed with "none").
We use the JavaScript String slice method. This method gets a portion of a string and returns that portion. It needs the start index and end index for the portion to be retrieved. Index counting of characters in a string begins from zero. The portion is actually copied from the original string, so the original string remains intact.
The string from which to get the portion is the otherSpanNo string. The portion sliced out has to be equal to the value of spanNo. The slice has to begin from index zero. The first statement in the if-block has the beginslice variable with a value of zero for this purpose. The length of the portion is the length of the value of spanNo. The second statement in the if-block has the endslice variable with value spanNo.length for this purpose. The next statement gets the portion of the string and assigns it to the slicedStr variable.