Reputation: 17522
I have this
...
<li style="padding: 50px;">
<a href="blabla" style="background:red;">Click Me</a>
</li>
...
As you all may know that the <a>
tag will be smaller in size.
I want to use jquery as so...
$('li').click(function (){
$(this).find('a').trigger('click');//does not work :(
alert($(this).find('a').attr('href'););//returns blabla//which is correct
});
Any ideas why the <a>
cannot be triggered in this way ?
PS
Upvotes: 0
Views: 362
Reputation: 40535
$(this).find('a').trigger('click')
Only triggers the javascript event , and not the default events
Here is the fix
$('li').click(function (){
var url = $(this).find('a').attr('href');
location.href= url
});
You could simplify it too:
$('li').click(function (){
location.href= $(this).find('a').attr('href');
});
Upvotes: 1
Reputation: 25249
With JavaScript you can't have the browser's native click simulated. You would have to setup an event handler for your link that indeed does get fired once you trigger 'click'. From there you could go with SLaks location = ...
method.
Upvotes: 0
Reputation: 18721
I think CSS is enough to do what you want...
What you want is just link tags take all <li>
space.
In many cases display:block;
on <a>
tags is enough but I don't know anything about your HTML/CSS ^^
Upvotes: 0
Reputation:
How about:
$(this).find("a:first").click();
I guess it's because you haven't bound the "a"'s onclick event to anything using jQuery :-)
Upvotes: 0
Reputation: 887479
jQuery's trigger
function will call event handlers registered using jQuery.
It will not simulate a real click.
Instead, you can write
location = $(this).find('a').attr('href');
Upvotes: 4