JavaScript code to add Dollar Sign to rows in matrix table

mchadwickITmchadwickIT MA, USACommunity Member Qubie ✭

JavaScript code to add Dollar Sign to rows in matrix table

Hi there, first time asking a question here as I've been looking around for an answer to this question and it looks like this is going to be a complex question that may require me to go to support.

What we are creating is a survey that is essentially a form to input data about basic personal investments. In order to accommodate a bulk entry from a single entity, we decided to go with a matrix table that has text entries in an 6x5 matrix. Now some of the rows will be Investment name which is text, year of first investment (number formatted to 4 digits), and then a dollar amount. Each column is an entry into this survey.

I'm able to format the year properly using javascript and I've found some code to add a dollar symbol to a column along with formatting a column using either cleave or wNumb to format the input for a column, however I'm unable to find to modify a row. For example this one for adding a $ sign to column "c4" and % to column "c5"

    function hasClass(element, cls) {
        return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
    }
 
    var inputs = $(this.getQuestionContainer()).select('input[type="text"]');
 
    for (var i = 0; i < inputs.length; i++) {
        var input = inputs[i];
        if(hasClass(input.up(), 'c4')) {
            $(input).insert({before: '$'});
            input.style.display = "inline";
        }   
        else if(hasClass(input.up(), 'c5')) {
            $(input).insert({after: '%'});
            input.style.display = "inline";
        }   
    }

For rows, I've inspected the HTML element in chrome and found it's identified as "QR~QID17~5~1~TEXT" but then I need to iterate through a row on load and insert the "$" inline like above.

For the code above, my understanding is the "hasClass" function is to find the elements within the question to search an element for the class "c4" and "c5"

Could I modify this to search for "QR~QID17~5~$i~TEXT" or is there a better way to do this? Or would it be better to just use wNumb to translate these answer fields into the dollar format that I want?

Sign In to Comment