Search the Community
Showing results for tags 'variable selectors'.
-
Please consider the following function and explain how to format the selectors for the highlighted jQuery objects. Please explain why you recommend what you do so that I need not ask the same question again further down the line. The names of the Javascript variables in question are, of course id and max. An iteration over the property-value pairs of a nested JSON object yields the following two pairs of property-value pairs for a single object: id : #letter_abstract and length : 150.. All other objects that are traversed by the iteration share the same property names -- only the values are different. The values of these two property-value pairs will be read into the function as id and max, respectively -- in effect, id = id and max = length Now, the jQuery objects in question are: $ (id textarea) -- where id is a Javascript variable corresponding to the value of the id attribute of an HTML div element, and textarea is the generic name of an HTML element. $(id span.display_count) -- where id is a Javascript variable corresponding to the value of the id attribute of the same HTML div element, and span.display_count is a generic HTML span element whose class attribute has the value of "display_count". $(id span.words_remaining) -- Same as above. Only the value of the class attribute is different. QUESTION: "How does one format the selectors properly?" function setWordContraint(id, max) { $(id textarea).on('keydown', function(e) { var words = $.trim(this.value).length ? this.value.match(/\S+/g).length : 0; if (words <= max) { $(id span.display_count).text(words); $(id span.words_remaining).text(max-words); }else{ if (e.which !== 8) e.preventDefault(); } }); }