n does not work in IE8 - Javascript

Asked

Viewed 153 times

4

Hello. I’m having problems with IE8, only in it, other browsers and different versions have been tested and worked, only in IE8 does not work.

I have a question page in YES/NO as I choose in radio buttons. Below a send button. When the user responds and clicks submit, play the questions with answers in the textarea. However, in IE8, do not accept the n in the textarea, so everything is in one line.

I’ve used \n, \r\n, \cr and even the code ASCII 13, and yet, IE8 doesn’t work. Someone could help me?

<script>
function PreencherCampo(){
    var procn1 =  document.getElementById("procn1").innerHTML;
    var pergunta1 =  document.getElementById("pergunta1").innerHTML;
    var pergunta2 =  document.getElementById("pergunta2").innerHTML;
    var pergunta3 =  document.getElementById("pergunta3").innerHTML;
    var procfield = document.getElementById("procfield").innerHTML;
    var pecsug = document.getElementById("pecsug").innerHTML;

    var elementos1 = document.getElementsByName('pergunta1'); 
    var resposta1 = '';
        for (i = 0; i < elementos1.length; i++) {
        if (elementos1[i].checked) {
        resposta1 = elementos1[i].value;
        }
    }

    var elementos2 = document.getElementsByName('pergunta2'); 
    var resposta2 = '';
        for (i = 0; i < elementos2.length; i++) {
        if (elementos2[i].checked) {
        resposta2 = elementos2[i].value;
        }
    }

    var elementos3 = document.getElementsByName('pergunta3'); 
    var resposta3 = '';
        for (i = 0; i < elementos3.length; i++) {
        if (elementos3[i].checked) {
        resposta3 = elementos3[i].value;
        }
    }

        document.getElementById("resposta").innerHTML = procn1 + '\r\n'+
                        pergunta1 + ': ' + resposta1 + '\r\n'+ 
                        pergunta2 + ': ' + resposta2 + '\r\n'+ 
                        pergunta3 + ': ' + resposta3 + '\r\n\n'+
                        procfield + '\r\n'+
                        pecsug;
}
</script>  
  • 1

    Post the HTML part, but as far as you can see \n is line breaking in text fields, with input and textarea, or element <pre>, but in html the correct would be <br>.

  • See if this solution works: http://stackoverflow.com/questions/27263614/new-line-character-n-not-supported-in-ie-8-browser

1 answer

2

As others have said you must use <br />, this is what semantically is correct in HTML since you are generating an HTML string.

You can also simplify your code and make it independent of the number of questions. A suggestion would be like this:

function PreencherCampo() {
    var procn1 = document.getElementById('procn1').innerHTML;
    var pecsug = document.getElementById('pecsug').innerHTML;
    var procfield = document.getElementById('procfield').innerHTML;

    var respostas = [1, 2, 3].reduce(function(str, nr) {
        var p = document.getElementById('pergunta' + nr).innerHTML;
        var r = [].filter.call(document.getElementsByName('pergunta' + nr), function(el) {
            return el.checked && el.value;
        })[0] || '';
        return [p, ': ', r, '<br/>'].join('');
    });
    document.getElementById('resposta').innerHTML = [procn1, respostas, procfield, pecsug].join('<br/>');
}

I haven’t tested, but if you have a bug do a jsFiddle with your HTML that I fix.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.