Rules

Secret ingredients to quality software

Edit
Info

Do you avoid using document.getElementById(id) and document.all(id) to get a single element, instead use selector $(#id)?

Last updated by Fiona Feng on 11 Jan 2018 09:16 am (over 3 years ago) See History

$(#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.

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")

Adam CoganAdam Cogan

We open source. This page is on GitHub