Consider the following snippet:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
</head>
<body>
<form>
<a id="a" href="http://google.com">Goooooogle</a>
</form>
<script>
$(function() {
var checkbox = $('<input type="checkbox"></input>');
checkbox.prependTo($('#a'));
checkbox.click(function(e) {
e.stopPropagation();
});
});
</script>
</body>
</html>
I want to check the box inside <a>and get the following behavior when clicked:
- Toggle the checkbox normally as usual
- Do something useful like an AJAX request
- Stay on this page, i.e. do not redirect to
a href
Also, I want not to override the default behavior if I click anywhere abut not on the checkbox. That is, I want to allow the execution of all event handlers associated with a amouse click.
I thought this should be pretty easy, but I can't get the behavior I want. Or:
- I will go to Google if I put the code.
- ,
e.preventDefault() of return false;. , checkbox.attr('checked', 'checked') .
?
UPD: Chrome, . , Firefox. - ?