Reputation: 1475
In a PHP page i have several rows of one table like this
echo '<tr><td><a href="#" onclick="myFunction('.$id.')">Click</a></td></tr>';
The $id
is dynamically generated from a database
So I want to define the function in jQuery but to pass the parameter to the jQuery function.
For each button I click there will be another parameter passed
Upvotes: 1
Views: 764
Reputation: 3431
you need to modify your html a bit:
echo '<tr><td><a class="someclass" href="#" id='".$id.'">Click</a></td></tr>';
then you can call it by it's class in JQuery and do what you want: "$(this)" will be a reference to the clicked item.
$(".someclass").live('click',function(e){ e.preventDefault(); alert($(this).text())});
Upvotes: 0
Reputation: 9148
As other poster started saying, bind a function to an event. Say you assign a css class to your a tags to make it easier:
echo '<tr><td><a class="specialLinks" href="#" onclick="myFunction('.$id.')">Click</a></td></tr>';
Then you would bind to your class like this:
$('.specialLink').bind('click', function() { this.preventDefault(); alert($(this.attr("id")); });
Upvotes: 0
Reputation: 1157
Well in a dirty way you can assign your id's in rel tag like this:
echo '<tr><td><a href="#" rel="'.$id.'" class="mylink">Click</a></td></tr>';
than you can search for mybutton class in jquery an add events to it:
$("a.mylink").click(function(){
alert($(this).attr('rel'));
});
So in this case $(this).attr('rel') should be your ID.
Upvotes: 0
Reputation: 712
What exactly do you want to do ?
Here's a sample function (it's not using jQuery!) to alert the user that the linked has been pressed and to stop propagating the event, so that it doesn't jump to another page on click
<script type="text/javascript">
function myFunction( param ) {
alert('The button with the id ' + param + ' has been pressed!');
return false;
}
</script>
Upvotes: 0
Reputation: 9929
Why not use the ID as an identifier for the link like this:
<a href="#" id="<?=$id?>" class="myjquerylink">Click me</a>
In jQuery you can bind to the onclick event like this:
// Execute on load $(document).ready(function(){ // Bind to click $('a.myjquerylink').click(function(){ // Get the id var id = $(this).attr('id'); // Do something with the id. doSomething(id); }); });
Upvotes: 1