Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2016
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    do not understand what .each() does here

    Hi guys,
    I was reading about jquery .each() function and I saw something I do not understand. I was wondering if someone could explain it to me
    It is a text field with the id person for example
    Code:
    $('#person').each(function(){
    var value = $(this).val()
    })
    I thought .each() was used to iterate over many elements. Is it also possible to use it on a single element? I do not understand what the person is doing here. Is it because the value of the field is dynamic?
    Thanks in advance for the helpful answers

  2. #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,473
    Thanks
    57
    Thanked 633 Times in 628 Posts
    your assumptions are correct - this is a very silly piece of code. In legal HTML, an ID must be unique to each element, and iterating over a collection of one is as pointless as it sounds. You would get exactly the same result from this one-liner:
    Code:
    var value = $('#person').val();

  3. Users who have thanked xelawho for this post:

    princesse (03-17-2016)


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •