jQuery Get Element By ID

jquery jQuery Get Element By ID Jquery Advantages

How to access the HTML tag or element using jQuery ?

Its very simple in jQuery to select element by Id as compared to JavaScript. Just start with $ symbol then pass the id value start with '#' into the parenthesis.

Syntax of get Element By id using jQuery


Get Element by id="mycontent" and display it in the HTML element with id="results" on button click

First we will get the Element from id="mycontent" then will store into the variable and then will assign the content value to the id="result" using jQuery function html() function.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button> Click Me </button> <div id="mycontent" >Welcome to CodingPk jQuery Tutorial Series</div> <div id="results" style="border: 1px solid red;">I am Div with ID = result </div> <script type="text/javascript"> $("button").click(function() { var mycontent = $("#mycontent").html(); $("#results").html(mycontent); }); </script>

Output of above Code

Welcome to CodingPk jQuery Tutorial Series
I am Div with ID = result
jquery Jquery Advantages Chapter Next »