Error caused by line break in append

1

In jquery, if I enter the following code, the error:

variavel.append('<div class="qualquer">
                    <div class="subdivs">
                        conteudo da subdiv
                    </div>
                    <div class="subdivs">
                        conteudo da subdiv
                    </div>
                </div>
                    ');

But if I put the same code without a line break, it will no longer be an error:

variavel.append('<div class="qualquer"><div class="subdivs">conteudo da subdiv</div><div class="subdivs">conteudo da subdiv</div></div>');

My question is

  

How to avoid this error caused by the line break? Is there a "proxy" pro append so I can insert code with line break?

     
    

I am trying to learn more about JQuery so I do not know the means to do such a thing!

  
    
asked by anonymous 31.08.2015 / 23:03

2 answers

1

The problem occurs because of line breaks that prevent the formation of string , which is the argument accepted by append() of jQuery . You can solve this by using \ at the end of each line, in which case you will need to change your double quotation marks ( " ) by simple ( ' ) in the internal contents as below:

variavel.append("<div class='qualquer'> \
                    <div class='subdivs'> \
                        conteudo da subdiv \
                    </div> \
                    <div class='subdivs'> \
                        conteudo da subdiv \
                    </div> \
                </div>");

result: "<div class='qualquer'> <div class='subdivs'> conteudo da subdiv </div> <div class='subdivs'> conteudo da subdiv </div> </div>"

Or you can also divide by lines, and concatenate one by one using character ( + ) as shown below:

variavel.append('<div class="qualquer">' +
                    '<div class="subdivs">' +
                        'conteudo da subdiv' +
                    '</div>' +
                    '<div class="subdivs">' +
                        'conteudo da subdiv' +
                    '</div>' +
                '</div>');

result: "<div class="qualquer"><div class="subdivs">conteudo da subdiv</div><div class="subdivs">conteudo da subdiv</div></div>"

The difference between the two is that in the first insert semi-columns at the end of each line, the second concatenates directly. Remember that both can be added to a variable and you can set the same in your append() .

    
31.08.2015 / 23:26
0

You can use \ n:

$('#teste').append('<div class="qualquer">\n<div class="subdivs">\nconteudo da subdiv\n</div>\n<div class="subdivs">\nconteudo da subdiv\n</div>\n</div>');

link

    
31.08.2015 / 23:31