JavaScript AJAX Basics (retiring) AJAX and APIs Stage 4 Challenge Answer

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

Question about tags:animal and selecting form element

Why is 'animal' the tags value and not the value of the search input? Also, why is the form element selected for the submit method instead of the actual submit button?

2 Answers

Steven Parker
Steven Parker
182,603 Points

The variable "animal" does contain the value of the search input. This happens on this line:

var animal = $searchField.val();

Here, "$searchField" is a jQuery object that refers to the input box, and the ".val" method gets the search input entered by the user.

The advantage of using the form as the source of the submit event is that the event can be generated more ways than just by using the button. In fact, in the video the button is not used, and the event is generated when the "Enter" key is pressed during the text input.