user1957694
user1957694

Reputation:

How to pass a variable from onclick another function?

I try to pass a variable from onclick another function. The content is created at runtime so I use on() of JQuery and don't know how to pass the variable.

<a class="call" href="#" onclick="call('.$id['id'].');">...</a>

Here the js:

$('.call').on("click", function(id) {
    alert(id);
});

The alert responds [object Object]

Are not you supposed to be like?

Upvotes: 0

Views: 621

Answers (2)

hsuk
hsuk

Reputation: 6860

Change:

<a class="call" href="#" onclick="call('.$id['id'].');">...</a>

To:

<a href="#" onclick="call('<?php echo $id['id']; ?>')">...</a>

and Change:

$('.call').on("click", function(id) {
    alert(id);
});

To:

function call(id)
{
    alert(id);
}

Upvotes: 1

mplungjan
mplungjan

Reputation: 177950

How about

<a class="call" href="#" id="'.$id['id'].'">...</a>


$('.call').on("click", function(e) {
    e.preventDefault(); // unless you need to follow the link too
    alert(this.id);
});

Upvotes: 1

Related Questions