Want to save a few lines of code but dont know how to do it
Hi all,
I am using JQuery 1.42 to write a simple popup table to show some information to users when they
move the mouse over a string of sentence.
Here is the code I copied from a web site:
- <script>
- $(document).ready(function(){
- $("#link").mouseover(function(){
- $("#popup_menu").show();
- return false;
- });
- $("#popup_menu").mouseout(function(){
- $(this).hide();
- return false;
- });
- });
- </script>
<a href="#" id="link">Main Message</a>
The code definitely works, but the script is confined to one link only.
What if there are tens of links like these:
<a href="#" id="link2">Message 2</a>
<a href="#" id="link3">Message 3</a>
...<a href="#" id="link100">Message 100</a>
In that case I have to write many scripts accordingly:
- <script>
- $(document).ready(function(){
- $("#link2").mouseover(function(){
- $("#popup_menu").show();
- return false;
- });
- $("#popup_menu").mouseout(function(){
- $(this).hide();
- return false;
- });
- });
- </script>
- <script>
- $(document).ready(function(){
- $("#link3").mouseover(function(){
- $("#popup_menu").show();
- return false;
- });
- $("#popup_menu").mouseout(function(){
- $(this).hide();
- return false;
- });
- });
- </script>
In Javascript I would simply write a function like this to save the code:
function(whichLinks){
$(document).ready(function(){
$("#whichLink").mouseover(function(){
$("#popup_menu").show();
return false;
});
$("#popup_menu").mouseout(function(){
$(this).hide();
return false;
});
});
}
But I don't know how to do this in JQuery.
Could you help me modify the code please?
Thank you