Do you avoid using document.getElementById(id) and document.all(id) to get a single element, instead use selector $(#id)?
Updated by Brady Stroud [SSW] 1 year ago. See history
123
<introEmbed
body={<>
`$(#id)` is a selector of jQuery. It gets the single element with the given id.
[jQuery](https://jquery.com/) 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.
</>}
/>
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: Figure - Bad Code
<h1 id="Head1">Hello</h1><script type="text/javascript" language="javascript">document.getElementById("Head1").style.color = "red";</script>
❌ Figure: Figure: Bad Code
<h1 id="Head1">Hello</h1><script type="text/javascript" language="javascript">$("#Head1").css("color", "red");</script>
✅ Figure: Figure: Good Code - Using $("#Head1")
Related rules
Need help?
SSW Consulting has over 30 years of experience developing awesome software solutions.