select input within a div by name

29,219

Solution 1

UPdated based on your update

var OriginalFiled = $('#TT_CartForm__form input[name="' + FieldName + '"]');

Try changing your quotations around to ensure string vaiable read. something else to keep in mind, IE7 and IE8 support attribute selectors only if a !DOCTYPE is specified. and in IE6- they dont work ata all

Also double check your element ID name, I notice it has two areas that are underscore and one area has 2 underscores. Make sure the div holding your input has that exact same id value

Solution 2

Slightly faster selector:

$('#second_update_form_data').find('input').each(function(index) {

    //find all input fields in the second div
    var fieldName     = $(this).attr('name'),
        fieldValue    = $(this).val(),

    //try to get the corresponding field from the original form to change it
        selector      = 'TT_CartForm__form input[name=' + fieldName + ']',
        originalFiled = $(selector);
Share:
29,219
DavidTonarini
Author by

DavidTonarini

Updated on November 07, 2020

Comments

  • DavidTonarini
    DavidTonarini over 3 years

    I need to update the value of some input fields of a form, using values from a second (dynamically generated) div.

    Here’s the code

    $("#second_update_form_data :input").each(function(index) {
    
    //find all input fields in the second div
                    var FieldName = $(this).attr("name");
                    var FieldValue = $(this).val();
    
      //try to get the corresponding field from the original form to change it
                    var Selector = "#TT_CartForm__form input[name=" + FieldName + "]";
                    var OriginalFiled = $(Selector);
    

    This does not work, but I’m not sure why. I could add an id to the original input fields and use that to select them, but I’d prefer to understand what’s wrong and use the correct selector.

    Thanks in advance, kind regards

    EDIT: I’m sorry, I realize I didn’t specified which part doesn’t work. It is the last line, when I try to select using Selector, so it’s this last one that seems to be wrong

  • Mark Pieszak - Trilon.io
    Mark Pieszak - Trilon.io over 11 years
    This wouldn't fix the issue, :input is deprecated jQuery, same as doing input, just slower.
  • DickieBoy
    DickieBoy over 11 years
    Didn't know about this. Incase anyone else finds this: bugs.jquery.com/ticket/9400 has more details
  • DavidTonarini
    DavidTonarini over 11 years
    How do you mean? I don’t need to attach a handler, I need to select an existing field.
  • DavidTonarini
    DavidTonarini over 11 years
    Ahah, pretty simple but could‘t see at all! :D Thank you very much, for the further info as well.