{"id":1361,"date":"2022-08-30T15:15:53","date_gmt":"2022-08-30T15:15:53","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/13\/javascript-making-variable-an-element-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:53","modified_gmt":"2022-08-30T15:15:53","slug":"javascript-making-variable-an-element-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/javascript-making-variable-an-element-collection-of-common-programming-errors\/","title":{"rendered":"javascript making variable an element-Collection of common programming errors"},"content":{"rendered":"<p>I would like to pass the name of the element i am playing with and then change its class. I call these functions from another function.<\/p>\n<pre><code>   function addErrClass(field){\n        field.removeClass(\"valid\");\n        field + Info.removeClass(\"valid\");\n        field.addClass(\"error\")\n        field + Info.addClass(\"error\");\n    }\n    function addValClass(field){\n        field.removeClass(\"error\");\n        field + Info.removeClass(\"error\");\n        field.addClass(\"valid\")\n        field + Info.addClass(\"valid\");\n    }\n<\/code><\/pre>\n<p>I call these functions by<\/p>\n<pre><code>if(data == \"yes\")\n          {\n            addErrClass('name');\n            nameInfo.text(\"Please choose another usename, \" + checkuser + \" is in use\");                \n          } else {\n             addValClass('name');\n             nameInfo.text(\"Awesome, \" + checkuser + \" is available\");  \n          }\n<\/code><\/pre>\n<p>each of the elements have been given a variable name:<\/p>\n<pre><code>var name = $(\"#nuser\");     var nameInfo\n   = $(\"#nuserInfo\");   var email = $(\"#email\");    var emailInfo =\n   $(\"#emailInfo\");\n<\/code><\/pre>\n<p>error received:<\/p>\n<p>Uncaught TypeError: Object name has no method &#8216;removeClass&#8217;<\/p>\n<p>essentially im trying to achieve the following with each function<\/p>\n<p>name.addClass or email.removeClass<\/p>\n<p>I will be using this on lots of fields, but thought id try and keep the code to a minimum for my problem.<\/p>\n<p>EDITED WITH ANSWER. THANKS<\/p>\n<pre><code>if(data === \"yes\")\n{\n    addErrClass(name);\n    addErrClass(nameInfo);\n    nameInfo.text(\"Please choose another usename, \" + checkuser + \" is in use\");                \n } else {\n     addValClass(name);\n     addValClass(nameInfo);\n     nameInfo.text(\"Awesome, \" + checkuser + \" is available\");  \n }\n<\/code><\/pre>\n<ol>\n<li>\n<p>You are passing &#8216;name&#8217; which is a string into the function. Since the String type does not have a method called &#8216;removeClass&#8217; you are receiving this error. This method only exists on jQuery objects.<\/p>\n<p>Since you said you had already instantiated a variable called name that is a jQuery object, try this:<\/p>\n<pre><code>if(data === \"yes\")\n{\n    addErrClass(name);\n    nameInfo.text(\"Please choose another usename, \" + checkuser + \" is in use\");                \n } else {\n     addValClass(name);\n     nameInfo.text(\"Awesome, \" + checkuser + \" is available\");  \n }\n<\/code><\/pre>\n<\/li>\n<li>\n<p>You seem to be passing the string &#8216;name&#8217; to your function instead of the variable name which contains your jquery object. There&#8217;s no removeClass method on the string object.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-13 09:49:47. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I would like to pass the name of the element i am playing with and then change its class. I call these functions from another function. function addErrClass(field){ field.removeClass(&#8220;valid&#8221;); field + Info.removeClass(&#8220;valid&#8221;); field.addClass(&#8220;error&#8221;) field + Info.addClass(&#8220;error&#8221;); } function addValClass(field){ field.removeClass(&#8220;error&#8221;); field + Info.removeClass(&#8220;error&#8221;); field.addClass(&#8220;valid&#8221;) field + Info.addClass(&#8220;valid&#8221;); } I call these functions by if(data == [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1361","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1361"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1361\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}