Javascript does not work when I try to do more than 2 tasks

Asked

Viewed 39 times

-1

I’m early in web development. The script below only works for what I do in the first for, I don’t know what I’m doing wrong. When he should do the second part is he doesn’t even get into it. It also doesn’t call Alert right after the first loop for. It’s like right after the first one he stops.

`

            var setUnid = document.getElementById('unidadeVenda').value;
            var setUnid2 = document.getElementById('unidadeVendaAux');
            var unidDesc = '{% getIdUnidade %}';
            var array = unidDesc.split(',');
            var descUnid;

            for (i = 0; i < array.length; i++) {
                descUnid = array[i].split(':');
                if (descUnid[1].trim() == setUnid) {
                    setUnid2.value = descUnid[0].trim();
                }
            }

            alert('TESTE'); 

            var setCom = document.getElementById('agente').value;
            var setCom2 = document.getElementById('agenteVendaAux');
            var comDesc = '{% getIdAgente %}';
            var arrayCom = comDesc.split(',');
            var descCom;


            for (i = 0; i < arrayCom.length; i++) {
                    descCom = arrayCom[i].split(':');

                    if (descCom[1].trim() == setCom) {
                        setCom2.value = descCom[0].trim();
                        alert(descCom[0].trim());
                    }
            }

  • 1

    Can you merge HTML? how do you know it doesn’t enter second for? some error on the console?

  • Good morning Sergio, I discovered that the problem is when I use Trim(). For example in the first is " descUnid[1]. Trim() " did not understand the reason, but if used in this way ".

  • What gives console.log(typeof descUnid[1]);?

  • console.log(typeof descUnid[1]); gives string, but I saw that one of the information gave "Undefined"

1 answer

1

I found that the problem is when I use Trim(). For example in the first is " descUnid[1]. Trim() " did not understand the reason, but if used this way " var desc = new String(descUnid[1]); "and then " desc.Trim() "my problem no longer occurs.

Browser other questions tagged

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