1

I am trying to show a jQuery alert by hovering on different links with different ids.

I want to tailor the alert based on each link hovered over. These links are created dynamically from a table...

Each link has a different id attribute, so I was thinking to have alert for each without having to click on the link.

For example: a link might have index.php?id=1 So I want to show an alert on hover that says This is an alert for link 1, etc.


Edit 1:

The div:

echo '<div class="trigger">';
            echo "<a class='trigger' href='".INDEX.'?categ='.$_GET['categ'].'&action='.$_GET['action'].'&subaction=viewlevels'.'&levelid='.$compi['Competence_ID']."'>";
            echo '<img class="linkki" src="'.KUVAT.'paivita.gif" alt="'._("tiedot").'" title="'._("What is this?").'"/></a>';
            echo '<div id="pop-up">';

            echo" <h3>Pop-up div Successfully Displayed for".$_GET['levelid'].
                    "</p></div>";

Edit 2:

<head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
        <script type="text/javascript">

        $('.trigger').mouseover(function() {
               alert("You are hovering over " + $(this).attr('href').match(/id=([0-9]+)/)[1]);
            });

        </script>

But it always tells me that levelid is undefined..( of course because the form has not been sent)

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
auicsc
  • 297
  • 1
  • 3
  • 14
  • 2
    Totally possible. What have you tried so far? Post your current code. – techfoobar Mar 28 '13 at 10:46
  • 1
    Change your anchor tag to have `class` "trigger" and put the id in the `id` field. (There can be no more than one tag with a certain ID, so your HTML is invalid). If that is not possible, check [this link](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values) on how to parse GET parameters in javascript. – Bart Friederichs Mar 28 '13 at 10:57

5 Answers5

3

Yes, you can use jQuery's mouseover() for this:

$('.trigger').mouseover(function() {
   alert("This is an alert for link " + $(this).attr('href').match(/id=([0-9]+)/)[1]);
});

You should change from using ID's to using a common class.

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
  • He said the `id attribute` would be in `href tag` so how will you show the id just by using `this.id`? – Dead Man Mar 28 '13 at 10:48
  • @DeadMan I answered too quickly as I wanted to mention mouseover, will change it up now – dsgriffin Mar 28 '13 at 10:51
  • your current answer will alert the full link instead of just id. – Dead Man Mar 28 '13 at 10:51
  • The thing is that the ID is not defined when the page loads the first time ! – auicsc Mar 28 '13 at 10:52
  • @user125697 ok. I am waiting to see your answer – Dead Man Mar 28 '13 at 10:54
  • @Arvind Yes then you can just use a class obviously, that is no excuse for a downvote, I answered the question the user asked I can't change what he wants.. – dsgriffin Mar 28 '13 at 10:57
  • 1
    @user125697 could you please change the answer to match my variables? I am very newbie to jquery.. thanks! – auicsc Mar 28 '13 at 11:00
  • @auicsc Please change trigger from an ID to a Class – dsgriffin Mar 28 '13 at 11:04
  • @user125697 I dont know why now it is not showing any alert anymore – auicsc Mar 28 '13 at 11:10
  • @auicsc Post all your code you have. Change id="trigger" to class="trigger", give it to every element. Then use my function. – dsgriffin Mar 28 '13 at 11:11
  • @auicsc Did you fix it in the end? – dsgriffin Mar 29 '13 at 11:24
  • No bro, instead I just used a very "ugly" solution by showing the data in a new window when each link is clicked.. Yet, it would be great if I could do it the way I described in the problem.. – auicsc Mar 29 '13 at 19:17
  • @auicsc Oh man that's unfortunate, I guess you found some solution at least! If I could have a way to test the PHP on my end I'm sure something could be done, it's just a shame it's not just using html/jquery – dsgriffin Mar 29 '13 at 19:32
  • Thats really fine:) Thanks a lot for your help, I really appreciate. Maybe you'll help me with other problems cuz I am sure i'll be posting many haha. – auicsc Mar 30 '13 at 10:37
1

to bind jquery function on a link over of container child element use below code

$(document).ready(function(){

     jQuery("#container a").each(function() {

         jQuery(this).mouseover(function() {
         alert(jQuery(this).attr('href'));
       });
    });

});

0
  $('#aid').mouseover(function(){alert('whatever you want'+this.id)});

documentation http://api.jquery.com/mouseover/

alwaysLearn
  • 6,882
  • 7
  • 39
  • 67
0

You can also use .hover and that has two callback one when hover-over and one for hover-out.

$('a').hover(function(){
  alert($(this).attr('href'));
},function(){
  alert('hover out');
});
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
0

If you are creating links dynamically, then associate attribute class (say sampleclass) and attribute id as (concatenate "link " and id value from database) with each link

Now

$(document).redy(function(){
  $(".sampleclass").hover(function(){
     alert("This is " + $(this).attr("id"));
  });
});
Arvind
  • 938
  • 9
  • 23