Dail
Dail

Reputation: 4606

How to get the href of a link using jquery?

I have a simple jquery code:

$(function(){                              
   $('#note_pag a').click(function(){      
    alert('click');                
    return false;                          
   });                                     
});            

and this is the html code:

<div id="note_pag">                               
  <ul>                                            
    <li><a href="1">1</a></li>                    
    <li><a href="2">2</a></li>                    
    <li><a href="3">3</a></li>                    
    <li><a href="4">4</a></li>                    
  </ul>                                           
</div>    

my question is: how can i get the href of the link clicked from the user?

Thanks

Upvotes: 0

Views: 337

Answers (2)

Sedat Başar
Sedat Başar

Reputation: 3798

$(function(){                              
   $('#note_pag a').click(function(){      
    alert($(this).attr('href'));                
    return false;                          
   });                                     
});

Check here http://jsfiddle.net/pHhw5/

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237817

Within the event handler, this is a reference to the element clicked. You can then use that DOM element to build a jQuery selection, and then use the attr function to get the value of the href attribute:

$(function(){                              
   $('#note_pag a').click(function(){      
    alert('cliccato link');  
    alert($(this).attr('href'));              
    return false;                          
   });                                     
});            

Upvotes: 3

Related Questions