There is a form:

<form class="js-ajax-form"> <!-- Ivan Triumphov code Get8(amoCRM) убрал action="/forms/sendmail.php" method="post" --> <input type="hidden" name="reachgoal" value="mainRightForm" class="js-reach-goal"> <input type="hidden" name="type_form" value="zayvka"> <input class="field" placeholder="Имя" name="fio"> <input class="field" placeholder="Телефон" name="phone" id="phone1"> </form> 

How to check if the form has a field? by name attribute
so $('.js-ajax-form').is('input[name="phone"]'); always false

  • because instead of is you need to use has - Grundy
  • yes for sure ... thanks))) - Ivan Triumphov

1 answer 1

The is method checks if the current collection of selected elements matches the specified selector.

Since the form is selected, it does not match the 'input[name="phone"]' selector and the result is naturally false.

Instead, you can use the methods find , filter , has

All these methods return a collection, therefore it will be necessary to check the number of remaining elements.

for example

 $('.js-ajax-form').has('input[name="phone"]').length > 0 

In this case, the field is present; if it is 0, it is absent.

  • Thanks, I think I will use this code - Ivan Triumphov