Search code examples
javascriptjqueryequivalent

What is the jQuery equivalent to document.forms[0].elements[i].value;?


What is the jquery equivalent to: document.forms[0].elements[i].value;?

I don't know how to travel through a form and its elements in jQuery and would like to know how to do it.


Solution

  • The usual translation is the :input selector:

    $("form:first :input").each(function() {
      alert($(this).val()); //alerts the value
    });
    

    The :first is because your example pulls the first <form>, if there's only one or you want all input elements, just take the :first off. The :input selector works for <input>, <select>, <textarea>...all the elements you typically care about here.

    However, if we knew exactly what your goal is, there's probably a very simple way to achieve it. If you can post more info, like the HTML and what values you want to extract (or do something else with).