Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select input within a div by name

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

like image 926
DavidTonarini Avatar asked Nov 06 '12 17:11

DavidTonarini


People also ask

How do I get all input elements in a div?

To get all input fields inside div with JavaScript, we select the container div with querySelector . Then we select all the input elements inside with querySelectorAll . to add inputs inside the div. to select the div with querySelector .

Can I put an input inside a div?

You can't place any element inside an input. An input does not contain any HTML. Show activity on this post. The way to do that is to put the input and the wanted div inside a wrapper div, which should look like the input - the wanted width, length of the input and should have border.

How to select all input in css?

Element Selectors The selector "input[type=text]" means select all inputs with the type attribute equal to text.

How do I select a div element?

Use document.querySelector on to select a div and then select an element within it with the given class after that. We just call querySelector on the element with the ID mydiv to select items inside that div. Therefore, innerDiv is the div with the class myclass .


1 Answers

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

like image 134
SpYk3HH Avatar answered Oct 13 '22 09:10

SpYk3HH