Patrioticcow
Patrioticcow

Reputation: 27058

How do I trigger a function?

I have this code:

<a href="#" class="button" id="buyme" ></a>  
<a href="#" id="buy" onclick="placeOrder('10');return false;"><span id="please">click me</span></a>

<script>
$('#buyme').click(function() {
    $('#buy').trigger(function(placeOrder('10')));
});
</script>

<script>
function placeOrder(var) {
.....
}
</script>

What I want to do is when I click on #buyme to trigger the onclick from the #buy link or to trigger the function from inside onClick.

My example doesn't seem to do it. Any ideas?

edit:

for some reason :

$('#buyme').click(function() {
$("#buy").click();
});

doesn't work

Upvotes: 1

Views: 85

Answers (4)

Ry-
Ry-

Reputation: 225281

This bit: function(placeOrder('10')) is invalid. Replace it with:

$('#buy').click();

Upvotes: 0

Digital Plane
Digital Plane

Reputation: 38294

Just use click(). According to docs, calling it without an argument triggers the event.

$('#buy').click();

Upvotes: 0

James Hill
James Hill

Reputation: 61872

Just call the function yourself:

$('#buyme').click(function() {
    placeOrder('10');
});

You could also trigger the click event of the button and let it call the function (seems a bit backwards though):

$('#buyme').click(function() {
    $("#buy").click();
});

Upvotes: 4

James Montagne
James Montagne

Reputation: 78780

$("#buyme").click(function(){
    $("#buy").click();
});

Upvotes: 0

Related Questions