Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "\n" create a new line even when told not to?

I am having a problem with "\n" creating a line even when it is told not to when copying. The fix is probably something simple that I am just not seeing for some reason. I would appreciate any input or coaching on this problem.

(Please only give me Javascript answers as I am not interested in jquery or other methods)

       <script type="text/javascript">

        if (pullDownResponseE == "")
        {
        }
        else {
        var pullDownValuesE = document.getElementById("taskOne");
        var pullDownResponseE = pullDownValuesE.options[pullDownValuesE.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponseE;
        }


        if (pullDownResponseF == "")
        {
        }
        else{
        var pullDownValuesF = document.getElementById("taskTwo");
        var pullDownResponseF = pullDownValuesF.options[pullDownValuesF.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponseF;
        }

        </script>

As you can see, pullDownResponseF and pullDownReponseE should do nothing if my dropdown value equals "" and this portion works for the most part, it doesn't execute any of the else code EXCEPT for the new line "\n" part.

Can anyone explain what is going wrong here?

EDIT: Having more code might help here. I'll only include the essential portions since it is so long.

        <script type="text/javascript">

        function copyNotesTemplate()
        { 

        var stuffToCopy = document.getElementById('myForm').value;
        if(stuffToCopy.length > 1)
        {
        var stuffToCopy = "PT meets criteria" + "\n" + document.getElementById('myForm').value;
        }
        if(document.getElementById('noPtCriteria').checked)
        {
        var stuffToCopy = document.getElementById('noPtCriteria').value;
        }


        if (pullDownResponsee == "")
        {
        }
        else {
        var pullDownValuese = document.getElementById("taskOne");
        var pullDownResponsee = pullDownValuese.options[pullDownValuese.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponsee;
        }


        if (pullDownResponsef == "")
        {
        }
        else{
        var pullDownValuesf = document.getElementById("taskTwo");
        var pullDownResponsef = pullDownValuesf.options[pullDownValuesf.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponsef;
        }

        if (pullDownResponseg == "")
        {
        }
        else{
        var pullDownValuesg = document.getElementById("taskThree");
        var pullDownResponseg = pullDownValuesg.options[pullDownValuesg.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponseg;
        }

        var tempValues = document.getElementById('whatUpdate').value
        if(tempValues.length > 1) 
        {
        var stuffToCopy = stuffToCopy + "Updated" + " " + document.getElementById('whatUpdate').value + " ";
        }
        else{
        }

        var tempValuess = document.getElementById('whatInfo').value
        if(tempValuess.length > 1) 
        {
        var stuffToCopy = stuffToCopy + "per" + " " + document.getElementById('whatInfo').value + "\n";
        }
        else{
        }

        var tempValue = document.getElementById('whatDSCRP').value
        if(tempValue.length > 1) 
        {
        var stuffToCopy = stuffToCopy + document.getElementById('whatDSCRP').value + " " + "dscrp on Collection tube and trf was resolved using" + " ";
        }
        else{
        }


        var tempValue = document.getElementById('resolveIT').value
        if(tempValue.length > 1) 
        {
        var stuffToCopy = stuffToCopy + document.getElementById('resolveIT').value + " ";
        }
        else{
        }

        var tempValue = document.getElementById('tubeCorrect').value
        if(tempValue.length > 1) 
        {
        var stuffToCopy = stuffToCopy + "trf was" + " " + document.getElementById('tubeCorrect').value;
        }
        else{
        }


        if(stuffToCopy.length > 1)
        {
        var stuffToCopy = stuffToCopy + "\n" + document.getElementById('moreNotes').value;
        }
        else{
        }

        if (pullDownResponsesu == "")
        {
        }
        else{
        var pullDownValuesu = document.getElementById("mod33Apply");
        var pullDownResponsesu = pullDownValuesu.options[pullDownValuesu.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponsesu;
        }

        if (pullDownResponsesb == "")
        {
        }
        else{
        var pullDownValuesb = document.getElementById("resultICR");
        var pullDownResponsesb = pullDownValuesb.options[pullDownValuesb.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponsesb + "," + " ";
        }

        if (pullDownResponsesc == "")
        {
        }
        else{
        var pullDownValuesc = document.getElementById("moneyNCIS");
        var pullDownResponsesc = pullDownValuesc.options[pullDownValuesc.selectedIndex].value;
        stuffToCopy = stuffToCopy + pullDownResponsesc + " ";
        }

        if (pullDownResponsesd == "")
        {
        }
        else{
        var pullDownValuesd = document.getElementById("resultMMT");
        var pullDownResponsesd = pullDownValuesd.options[pullDownValuesd.selectedIndex].value;
        stuffToCopy = stuffToCopy + pullDownResponsesd;
        }


        if(stuffToCopy.length > 1)
        {
        var stuffToCopy = stuffToCopy + " " + "Reason:" + " " + document.getElementById('whyNotEligible').value;
        }
        else{
        }

        if (pullDownResponsesa == "")
        {
        }
        else{
        var pullDownValuesa = document.getElementById("testReleased");
        var pullDownResponsesa = pullDownValuesa.options[pullDownValuesa.selectedIndex].value;
        stuffToCopy = stuffToCopy + "\n" + pullDownResponsesa;
        }
        window.clipboardData.setData('text', stuffToCopy);
        }
        </script>

If somebody skips filling out a note field or skips a dropdown in this example then it will not execute the code like I intended but it does create a new line when copied like this:

taskOne selected

(extra line here since task two wasn't selected)

taskThree selected

I would like there not to be an extra line between Task one and three if task two is skipped. Like this:

taskOne selected

taskThree selected

Note: I know that having else {} is pointless but it helps me visually.

I created snips of exactly what it looks like when copy/pasted from my tool that you can view here if you would like:

Example 1: http://imgur.com/wGO5vnT

Example 2: http://imgur.com/UX1tG5S

Here is an example of my html as well:

<html lang="en">
  What tasks are needed for the case?
  <br />
  <select class="style3" id="taskOne">
  <option value=""></option>
  <option value="ABN needed">ABN needed</option>
  <option value="Auth needed">Auth needed</option>
  </select>
  </html>
like image 534
Anakin Dawson Avatar asked Feb 07 '16 01:02

Anakin Dawson


Video Answer


2 Answers

No, it doesn't add a new line, see:

stuffToCopy = "";
controlGroup = "a\nb";
pullDownResponseE = "";

if (pullDownResponseE == "")
{
}
else {
    var pullDownValuesE = "taskOne";
    var pullDownResponseE = "value";
    stuffToCopy = stuffToCopy + "\n" + pullDownResponseE;
}
alert("stuffToCopy:"+stuffToCopy+";(no new-line here)\ncontrolGroup:"+controlGroup);

My guess is that your html is printed in such away that the values you get from the inputs contain an extra new-line at the end. Try changing your html to be 1 line, without new-lines, and test again.

Instead of:

<option value="a
">b
</option>

try:

<option value="a">b</option>
like image 78
Gavriel Avatar answered Sep 23 '22 11:09

Gavriel


Alright so I fixed it, should have used the almighty document.getElementById instead of attempting to use pullDownReponse for my if statements..

I simply changed the if statements like this:

 if (pullDownResponseg == "")
 {
 }

To this:

if (document.getElementById("taskThree").value == "")
 {
 }

Thanks for the help from the sincere. (and ridiculous non-answers from the others)

like image 36
Anakin Dawson Avatar answered Sep 23 '22 11:09

Anakin Dawson