Do you avoid using document.getElementById(id) and document.all(id) to get a single element, instead use selector $(#id)?
Rules to Better Website Development - ASP.NET|d1c7c6c6-dfe7-414b-a26a-771be26ab596;Rules to Better JavaScript and jQuery|c7f61c73-def9-41b0-9ab4-266c8d1b1ea6
v2.0
Posted at
11/01/2018 8:16 PM by
Fiona Feng
Rule Intro
$(#id) is a selector of jQuery. It gets the single element with the given id.
jQuery is a fast and concise JavaScript Library that simplifies how you traverse HTML documents, handle events, perform animations, and add Ajax interactions to your web pages. jQuery is designed to change the way that you write JavaScript.
Page Content
With jQuery, you can write less code but do more work.
<h1 id="Head1">Hello</h1>
<script type="text/javascript" language="javascript">
document.all("Head1").style.color="red";
</script>
Figure - Bad Code
<h1 id="Head1">Hello</h1>
<script type="text/javascript" language="javascript">
document.getElementById("Head1").style.color="red";
</script>
Figure: Bad Code
<h1 id="Head1">Hello</h1>
<script type="text/javascript" language="javascript">
$("#Head1").css("color","red");
</script>
Figure: Good Code - Using $("#Head1")
{9DDCB98A-A721-4191-AF87-3109043184F8}
Do you feel this rule needs an update?