I have this anchor and on click I would like to popup something. This href is within a page that has other hrefs.
<a class="sign_new" href="#sign_up">Sign up</a>
jQuery:
$(document).ready(function(){
$('a[href = "sign_up"]').click(function(){
alert('Sign new href executed.');
});
});
The above code does not fire up.
This question is related to
javascript
jquery
html
If you own the HTML code then it might be wise to assign an id to this href. Then your code would look like this:
<a id="sign_up" class="sign_new">Sign up</a>
And jQuery:
$(document).ready(function(){
$('#sign_up').click(function(){
alert('Sign new href executed.');
});
});
If you do not own the HTML then you'd need to change $('#sign_up') to $('a.sign_new'). You might also fire event.stopPropagation() if you have a href in anchor and do not want it handled (AFAIR return false might work as well).
$(document).ready(function(){
$('#sign_up').click(function(event){
alert('Sign new href executed.');
event.stopPropagation();
});
});
Try:
$(document).ready(function(){
$('a .sign_new').click(function(){
alert('Sign new href executed.');
});
});
You've mixed up the class
and href
names / selector type.
You are binding the click
event to anchors with an href attribute with value sign_new
.
Either bind anchors with class sign_new
or bind anchors with href value #sign_up
. I would prefer the former.
Source: Stackoverflow.com