{"id":1402,"date":"2022-08-30T15:16:14","date_gmt":"2022-08-30T15:16:14","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/15\/bind-jquery-events-to-div-with-specific-html-tag-like-textarea-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:14","modified_gmt":"2022-08-30T15:16:14","slug":"bind-jquery-events-to-div-with-specific-html-tag-like-textarea-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/bind-jquery-events-to-div-with-specific-html-tag-like-textarea-collection-of-common-programming-errors\/","title":{"rendered":"Bind jQuery events to DIV with specific HTML TAG like TEXTAREA-Collection of common programming errors"},"content":{"rendered":"<p>I have one <code>DIV<\/code> element which contains dynamically created <code>TEXTAREAs<\/code> and and .<\/p>\n<p>Currently I have bound three events on above <code>DIV<\/code> like below<\/p>\n<pre><code>jQuery(\"#uniqueId \").bind(\"click change keypress\", function(e){\n    ....\n    ....\n});\n<\/code><\/pre>\n<p>Now problems occur are when I click on <code>file<\/code> input button to Browse and Upload a file or check on Checkbox I receives following error<\/p>\n<pre><code>Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable. \n\nmyxFunction myXjavscript.js:1172\n(anonymous function) myXjavscript.js:109\nf.event.dispatch jquery-1.7.1.min.js:3\nh.handle.i\n<\/code><\/pre>\n<p>and because of it <code>file<\/code> and <code>checkbox<\/code> inputs are not working and throwing errors. I just want to bind above events only on <code>TEXTAREA<\/code> and not other elements under a <code>DIV<\/code>.<\/p>\n<p>Reason to use <code>\"click change keypress\"<\/code> events together because in Chrome I am performing Dynamic operations on Textarea to get cursor position after add\/edit\/delete texts from Textarea. So to record all those things I needed to add those three events.<\/p>\n<p>How can I achieve this?<\/p>\n<ol>\n<li>\n<p>If you only want to bind the events to the textareas contained within #uniqueId, use the second argument (delegate selector):<\/p>\n<pre><code>jQuery(\"#uniqueId \").on(\"click change keypress\", \"textarea\", function(e){\n    ....\n    ....\n});\n<\/code><\/pre>\n<p>Edit: didn&#8217;t realize you were using .bind, always use .on! It&#8217;s awesome. http:\/\/api.jquery.com\/on\/<\/p>\n<\/li>\n<li>\n<p>You should check the element before using it like,<\/p>\n<pre><code>if(jQuery(\"textarea#uniqueId\").length)\n{\n  jQuery(\"textarea#uniqueId\").bind(\"click change keypress\", function(e){\n      ....\n      ....\n  });\n}\n<\/code><\/pre>\n<p><strong>Or<\/strong> if the element is exists in page then write your code in document ready function like,<\/p>\n<pre><code>$(function(){\n  jQuery(\"textarea#uniqueId\").bind(\"click change keypress\", function(e){\n      ....\n      ....\n  });\n});\n<\/code><\/pre>\n<p>Also <strong>id must be unique<\/strong> if you <code>id<\/code> <code>conflicts<\/code> with other <code>elements<\/code> the you should specify <code>element<\/code> with id like <strong>textarea#uniqueid<\/strong><\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-15 09:09:24. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have one DIV element which contains dynamically created TEXTAREAs and and . Currently I have bound three events on above DIV like below jQuery(&#8220;#uniqueId &#8220;).bind(&#8220;click change keypress&#8221;, function(e){ &#8230;. &#8230;. }); Now problems occur are when I click on file input button to Browse and Upload a file or check on Checkbox I receives [&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-1402","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1402","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=1402"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1402\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}