You are viewing limited content. For full access, please sign in.

Question

Question

javascript is not returning the correct class names for input fields

asked on September 25, 2017

In my function, when specifying to work with each input field by class name, then return the class name, I get a different set of class names then the ones I have assigned.

It's strange that I can reference the field by a class name in the for each statement, but when I ask it to tell me it's class, it doesn't even return the class name that I used to find the field to begin with.

The reason I need to know it's class, is so that I can know which field I am on, as there is an unknown number of them on the form preventing me from referencing by iteration. Although the fields share the myClass (which I just used as an example) they also have unique class names.

This works with tables, but not with input fields.

$('.myClass input').each(function () {

       var str = $(this).attr('class');
       alert(str);

});

Here is what it returns: singleline cf-small lf-no-validate

None of which include my custom classes that I have assigned.

0 0

Answer

SELECTED ANSWER
replied on September 25, 2017

Hi Chad,

The element find by $('.myClass input') is the input element which does not contain the custom CSS, while the custom CSS is applied to the li element:

The blank space in '.myClass input' means finding the descendant input element of an element with class 'myClass', see https://api.jquery.com/descendant-selector/

To find the li element from the input element, use var str = $(this).closest('li').attr('class');

Note that there is no li element for fields inside table so to find class for field in table, use var str = $(this).closest('td').attr('class');

1 0
replied on September 26, 2017

Thank you very much, also explains the " input" and " select" requirement. I always thought it was just an extra requirement to verify what type of field your working with. I have been thinking of each field we drop on the form as a single object.

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.