Doubt on function . length

Asked

Viewed 180 times

1

In my case I’m picking up how many records are within the variable. But the function .length determines that when there is only one record it is undefined, it should show that there is a record in it. Is this a mistake of the function itself or is there a way to fix it? Or is there some other method I can use?

I’m storing it like this:

function insert_info(){
    w_qtde_info = document.forms['my_form'].blc_info.length;

}

    <form name="my_form"  method="post">
    <table width="100%">
    <tr>
    <td>
<?php
        while($w_cont <= $w_opc)
        { 
        print('<input type="radio" name="blc_info" value="">');
         } 
        print('<input type="button" onClick="insert_info();" value=">>">');
?>
    </td>
    </tr>
    </table>
    </form>

w_opc is determined by the user. That is, will add the radios (as per example) according to the value determined by the user!

Obs: I took the php part as a reference : Text radio

  • 2

    i would like to see the body of your form.... has how Voce post?

  • 2

    What is blc_info? an input field?

  • Yes, blc_info is an input!

  • 1

    length in this case it is not to be used as a function, but rather as a property. I also found nothing in the specification about this behavior you describe. This length is native or is it something that someone on your team has implemented?

  • 1

    @Renan he is very native! Because I ended up testing with other items (radio with certain values) and it occurs the same error!

  • 1

    I tested it here in Chrome and this property always gives Undefined for inputs and textareas. You speak of "records within a variable" - are these records actually properties that you are entering into the input? If you have more code that moves this input, edit the question to include this, ok?

  • 1

    Bruno, so what you want is value of him no? like w_qtde_info = document.forms['my_form'].blc_info.value;

  • No @Sergio, I need to know how many records were "included" within the variable! I’m going to post an example with radio that I think is easier!

  • 1

    Bruno, that while is PHP?

  • @Sergio, Yes (I edited a bit fast)!

  • 1

    In this case your javascript is correct, what you have is errors in PHP. Missing a echo before HTML and missing open and close PHP... know how to do? another question is your function does not return anything. It is complete?

  • Yes @Sergio it is complete. Missed the opening of PHP because I finished editing fast and I forgot to put here!

  • Bruno, this function does nothing. That is, it does not answer anything. What do you want to do with the number that this variable has? At least one alert(w_qtde_info); within the function?

Show 8 more comments

1 answer

1

Why don’t you do it?

   function insert_info(){
        w_qtde_info = document.forms['my_form'].blc_info.length || [];
    }

With this if your query returns Undefined you will not break a query on w_qtde_info.length

  • But @felipekm, is that if there is only one record inside the variable, it should show that there is this single record!

  • I don’t understand, what returns this object? Document.Forms['my_form']. blc_info

  • Document.Forms['my_form']. blc_info.length returns how many records it contains in blc_info!

  • 1

    Yes, I understood that, however, You say: "But the function . length determines that when there is only one record it is Undefined, "this is not true, LENGTH will never return Undefined. The only error you can find there is 'Document.Forms['my_form']. blc_info' is Undefined, so I suggested the answer like this.

  • In this case you are saying that Document.Forms['my_form']. blc_info is not taking the form values ?

  • Probably, with this validation above you eliminate the possibility of "popping" your app when consulting the Speed Length of this Array. Good Luck

Show 1 more comment

Browser other questions tagged

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