Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting "undefined" when trying to print id values from form elements in JQuery?

This is my form:

<form action="/Admin/usuarios/obtener_valores" method="POST">
<div class="span-16">
    <input type="hidden" value="1" name="usuarioPinIdHidden" id="usuarioPinIdHidden">
    <table class="summary" style="border: none;">
        <tr>
            <td colspan="2">¿A que edad tuvo sus primeras vacaciones?</td>
        </tr>
        <tr>
            <td colspan="2"><input id="text@&#39;+1+^+1" name="respuesta[0].respuesta" type="text" value=""/></td>
        </tr>
        <tr>
            <td colspan="2">¿Estaba casado en 1991?</td>
        </tr>
        <tr>
            <td colspan="2"><input id="boolean@&#39;+3+^+1" name="respuesta[1].respuesta" type="radio" value="true"/>
                <label for="boolean@'+3+^+1">Si</label>
                <br />
                <input id="boolean@&#39;+3+^+1" name="respuesta[1].respuesta" type="radio" value="false"/>
                <label for="boolean@'+3+^+1">No</label></td>
        </tr>
    </table>
</div>

This is a function to print all the id values after clicking the submit button:

$(document).ready(function() {
        $('form').submit(function (){
            return verificarRespuestas();
        });
    });

    function verificarRespuestas(){
        alert('hola');
        $('form').children().each(function(){
            var child = $(this);
            alert(child.id);
        });
        return false;
    }

but this is what it's printed:

  • hola
  • undefined
  • undefined

when I expect to get these values:

  • hola
  • text@'+1+^+1
  • boolean@'+3+^+1

What am I doing wrong?

Thanks in advance!

like image 439
Lucas Avatar asked Dec 17 '22 01:12

Lucas


2 Answers

  • children only selects the immediate children to the element, you want $('form').find('input') or $('form').find('input[id]') to only get those that have an ID, or $('form').find('input:visible') to only get those that are visible (ie, not hidden.)
  • Once there, you can simply do this.id in the each. Or, through jQuery, $(this).attr('id').
  • You have invalid IDs - not only do they have invalid characters, you cannot duplicate them as you do for the second input. What is the point of all the gibberish?

There are many smarter ways to denote field types in your inputs if you are doing some sort of client-side validation or styling, most commonly through classes but also through custom attributes or field types in HTML5. IDs are definitely not where you want to be putting booleanXXX information. The HTML spec says:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

like image 192
Paolo Bergantino Avatar answered May 20 '23 01:05

Paolo Bergantino


Description

You should use the jQuery attr() function to get the id.

jQuery.attr() Get the value of an attribute for the first element in the set of matched elements.

Sample

alert(child.attr('id');

More Information

  • jQuery.attr()
like image 35
dknaack Avatar answered May 19 '23 23:05

dknaack