in Education by
I'm trying to change the color of text lines containing "Accepted" to green and all others to red but when i upload my file all the lines are green. What am I doing wrong? I really don't know why the else statement is not read. document.getElementById("openFile").addEventListener("change", function(){ var file = this.files[0]; var fr = new FileReader(); fr.onload = function(){ document.getElementById("fileContents").textContent = this.result; let lines = document.getElementById("fileContents").textContent.split('\n'); let parser =''; for(let line = 0; line < lines.length; line++){ if (lines[line].substring(0,3)==="ACK"){ parser += lines[line].substring(0,3) + "\t" + lines[line].substring(49,108).trim() + "\t" + lines[line].substring(109,115) + "\t" + lines[line].substring(149,159).replace(/as/gi,"\tAccepted").replace(/rj/gi,"\tRejected") + '\n'; } else if (lines[line].substring(0,3)==="MSG"){ parser += lines[line].substring(0,3) + "\t" + lines[line].substring(35,159).replace(/,/,"").trim() + '\n'; } } document.getElementById("fileContents").textContent = parser; if (parser.includes("Accepted")){ document.getElementById("fileContents").style.color = "#00FF00"; } else { document.getElementById("fileContents").style.color = "#FF0000"; } } fr.readAsText(file); },false) <!DOCTYPE html> Parser




      

  



Run code snippetExpand snippet

After the suggestion of @aptriangle I tried to replace replace each line of text generated by an HTML element, in this case each substring should be a . I'm stuck on this and I can not make the necessary change. Can someone give me a help? Thank you in advance.

document.getElementById("openFile").addEventListener("change", function(){

    var file = this.files[0];

    var fr = new FileReader();

    

    fr.onload = function(){ 

        document.getElementById("fileContents").textContent = this.result;  

        

        let lines = document.getElementById("fileContents").textContent.split('\n');

        let parser =''; 

        

        for(let line = 0; line < lines.length; line++){

            if (lines[line].substring(0,3)==="ACK"){

                

                var sub1 = lines[line].substring(0,3) + '\t';

                var sub2 = lines[line].substring(49,108) + '\t';

                var sub3 = lines[line].substring(109,115) + '\t';

                var sub4 = lines[line].substring(149,lines.length).replace(/as/gi,"\tAccepted").replace(/rj/gi,"\tRejected").trim() + '\n';

                

                var tdata1,tdata2,tdata3,tdata4 = document.createElement("td");

                tdata1.createTextNode(sub1);

                tdata2.createTextNode(sub2);

                tdata3.createTextNode(sub3);

                tdata4.createTextNode(sub4);

                parser += tdata1 + tdata2

                    + tdata3 + tdata4;

                var element = document.getElementById("fileContents");

                element.appendChild(parser);

            }

            else if (lines[line].substring(0,3)==="MSG"){

                var sub1 = lines[line].substring(0,3) + '\t';

                var sub2 = lines[line].substring(35,lines.length).replace(/,/,'').trim() + '\n';

                

                parser += sub1 + sub2;

            }     

        }

        document.getElementById("fileContents").textContent = parser;

        

         

    }  

    fr.readAsText(file);

},false)

<!DOCTYPE html>



  

    

    

    

    Parser

  

  

    



    





      

  



Run code snippetExpand snippet

I still can't put paragraphs when i upload the code...now what am i doing wrong? Sorry to bother but i really can't figure it out.

document.getElementById("openFile").addEventListener("change", function(){

    let file = this.files[0];

    let fr = new FileReader();

    

    fr.onload = function(){ 

        let content = document.getElementById("fileContents");

        content.textContent = this.result;  

        

        let lines = content.textContent.split('\n');

        let parser = ''; 

        let para;

        

        for(let line = 0; line < lines.length; line++){

            

            para = document.createElement("p")

            para.innerHTML = lines[line];

            

            if (lines[line].substring(0,3)==="ACK"){    

                let sub1 = lines[line].substring(0,3) + '\t';

                let sub2 = lines[line].substring(49,108) + '\t';

                let sub3 = lines[line].substring(109,115) + '\t';

                let sub4 = lines[line].substring(149,lines.length).replace(/as/gi,"\tAccepted").replace(/rj/gi,"\tRejected") + '\n';

                

                parser += sub1 + sub2 + sub3 + sub4;

            }

            else if (lines[line].substring(0,3)==="MSG"){

                let sub1 = lines[line].substring(0,3) + '\t';

                let sub2 = lines[line].substring(35,lines.length).replace(/,/,'').trim() + '\n';

                

                parser += sub1 + sub2;    

            } 

            content.appendChild(para);

        }

        content.textContent = parser;

    }  

    fr.readAsText(file);

},false)

<!DOCTYPE html>



  

    

    

    

    Parser

  

  

    



    





      

  



Run code snippetExpand snippet

JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Here's a fiddle that shows a working split and style: https://jsfiddle.net/4qv3h0xp/ var element; for (var i = 0; i < split.length; i++) { element = document.createElement("p"); element.innerHTML = split[i]; if (split[i].indexOf("Accepted") == -1) { element.style.color = "#00FF00"; } else { element.style.color = "#FF0000"; } output.appendChild(element); } It creates a new element for every line and appends the elements to the parent, instead of styling the parent.

Related questions

0 votes
    What is not true in context of HTML tag ? O Table cell changes its width automatically based on content O We can ... table border is o Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    It took me forever to reduce the problem to this. I cannot express the amount of frustration I'm ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    Is there a way to specify the color a text is printed within Idle for Python 3.2? I'm looking ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    Is there a way to specify the color a text is printed within Idle for Python 3.2? I'm looking ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    How to change Status Bar text color in iOS?...
asked Mar 9, 2021 in Technology by JackTerrance
0 votes
    I can't make td "Date" to have fixed height. If there is less in Body section td Date element ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    I can't make td "Date" to have fixed height. If there is less in Body section td Date element ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I am trying to set text and value for both td tag and input tag at the same time. But after ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I am trying to set text and value for both td tag and input tag at the same time. But after ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    (This is a random image of showing a Dialog found on the Internet.) I've been implementing a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    (a) Which of the following is used to change the color of hyperlink from blue to any other color? (i) Link ( ... of these (iii) IFRAME Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    $ (a) Which of the following is used to change the color of hyperlink from blue to any other color? (i) Link ... (iv) none of these 00 Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    When plain text is converted to unreadable format, it is termed as _____________ (a) rotten text (b) raw ... -for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Oct 31, 2021 in Education by JackTerrance
...