1. Fetching coordinates of current location of the mouse:
Below is the code to retrieve the coordinates of the mouse at any position on the webpage and display inside a div with id getCoords:
<script> $(document).ready(function() { $(document).mousemove(function(e){ $('#getCoords').html("X: " + e.pageX + " and Y : " + e.pageY);}); }); </script> <div id=getCoords></div>
2. Using data() method to attach some data to an element:
Use of data() function is to attach/store an information into an element. To set the value, we need to pass two values, that is, a key and a value pair.
To retrieve the stored value, we call the data() function by passing key of the required value. Below is the sample code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#button1").click(function(){ $("#show").data("sometext", "Welcome user!!"); // stores data in show div having key as ‘sometext’ }); $("#button2").click(function(){ $("#text1").text($("#show").data("sometext")); // fetches data in text1 div using the key}); }); </script> </head> <body> <button id="button1">Click to attach text in hidden div</button> <button id="button2">Click to view welcome text</button><br> <br> <div id="text1"></div> <div id="show"></div> </body>
3. Disabling right mouse click on a webpage:
Many times, we come across a requirement to disable the right click feature on our webpage. To achieve this functionaligoodty, below code can be used:
$(document).ready(function(){ $(document).bind( "contextmenu", function(f){ return false; }); });