{"id":245,"date":"2009-10-30T18:47:00","date_gmt":"2009-10-30T18:47:00","guid":{"rendered":"http:\/\/blog.trungson.com\/?p=245"},"modified":"2009-10-30T18:47:00","modified_gmt":"2009-10-30T18:47:00","slug":"jquery-avoid-duplicate-elements-ids","status":"publish","type":"post","link":"http:\/\/blog.trungson.com\/?p=245","title":{"rendered":"jQuery &#8211; Avoid duplicate element&#8217;s IDs"},"content":{"rendered":"<p>If the input element has an ID and it is duplicate with another element, it&#8217;s likely cause an error. In this case, Firefox 3.5 and IE8 on Windows Vista has no problem while same browsers in Windows XP does not work.<\/p>\n<h4>Problem<\/h4>\n<p><textarea name=\"code\" class=\"html\"><\/p>\n<form id=\"myform\">\n<input name=\"action\" id=\"action\" value=\"doSomething\"><br \/>\n&#8230;<br \/>\n<\/form>\n<form id=\"anotherform\">\n<input name=\"action\" id=\"action\" value=\"doSomethingElse\"><br \/>\n&#8230;<br \/>\n<\/form>\n<p>&#8230;<br \/>\n$(&#8220;#myform #action&#8221;).val();<br \/>\n<\/textarea><\/p>\n<p>That works in Vista and Mac OSX but not XP. It&#8217;s rather weird it&#8217;s a problem that depends on the OS and not the browser. Anyway, it&#8217;s a mistake and duplicate IDs should always be avoid.<\/p>\n<h4>Solution<\/h4>\n<p><textarea name=\"code\" class=\"html\"><\/p>\n<form id=\"myform\">\n<input name=\"action\" value=\"doSomething\"><br \/>\n&#8230;<br \/>\n<\/form>\n<p>&#8230;<br \/>\n$(&#8220;#myform input[name=&#8217;action&#8217;]&#8221;).val();<br \/>\n<\/textarea><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If the input element has an ID and it is duplicate with another element, it&#8217;s likely cause an error. In this case, Firefox 3.5 and IE8 on Windows Vista has no problem while same browsers in Windows XP does not work. Problem &#8230; &#8230; &#8230; $(&#8220;#myform #action&#8221;).val(); That works in Vista and Mac OSX but [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts\/245"}],"collection":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=245"}],"version-history":[{"count":0,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts\/245\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}