[i]. val is not a Function

Asked

Viewed 23 times

2

I have the following code snippet:

var arrayPost = $("#formPlano13").serializeArray();
var arrayCBDDD = $("#formPlano13 input[name='cbDDD[]']").serializeArray();
var trDDD = '';

// *  Geramos um loop para os DDDs escolhidos
$.each(arrayCBDDD, function(k, v){

    qtdDDD = $('input[name=txQtdDDD' + v.value + ']').val();
    qtdPortabilidade = $('input[name=txQtdPortabilidadeDDD' + v.value + ']').val();
    trDDD += '<tr><td><strong>DDD ' + v.value + '</strong><br />Qtd Números: ' + qtdDDD + '<br /></td><td>R$</td></tr>';

    if(qtdPortabilidade > 0){

        trDDD += '<tr><td><strong>Portabilidade DDD ' + v.value + ':</strong><br />';
        varNumero = $('input[name=\'txPortabilidadeNumero' + v.value + '[]\']');

        for(i = 0; i < varNumero.length; i++){
            trDDD += '- ' + varNumero[i].val() + '<br>';
        }

        trDDD += '</td></tr>';

    }

});

console.log(arrayPost);
console.log(trDDD);

ArrayPost values (can dynamically increase or decrease)

0: {name: "cbDDD[]", value: "011"}
1: {name: "txQtdDDD011", value: "1"}
2: {name: "cbPortabilidade011", value: "on"}
3: {name: "txQtdPortabilidadeDDD011", value: "1"}
4: {name: "txPortabilidadeNumero011[]", value: "(11) 11111-1111"}

ArrayCBDDD values (can dynamically increase or decrease)

0: {name: "cbDDD[]", value: "011"}

When executing it, I’m getting the error: $(...)[i]. val is not a Function

How can I capture the value of the varNumber that comes from this loop? I’m having problems because this input is a multiple array (it can receive several values).

  • 1

    I think what you’re looking for is varNumero.eq(i).val() or varNumero[i].value. You can test?

  • 2

    varNumero[i] will return an html element, which does not have the function .val(). In this case, you can access with .value, or, if you want to continue with the object jquery, use .eq(i)

  • 2

    @Sergio always faster than me haha :)

  • 2

    @Arturotemplario :)

  • I’ll test and I’ll be right back! D

  • 1

    Worked perfectly!

  • I know it sounds stupid what I’ll ask, but how do I mark your answer, @Sergio as answered?

  • @Maykelesser the question is quite common. I’ve already marked it as duplicate from another previous question. You don’t need to do anything else :)

  • 1

    Thank you very much! ;-)

Show 4 more comments
No answers

Browser other questions tagged

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