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

Last updated by Brook Jeynes [SSW] 9 months 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")

We open source. Powered by GitHub