Ste
Ste

Reputation: 685

Passing in parameters to jQuery .click

I have a list, populated from rows in a MySQL database and I want to flag those rows in the database when the link is clicked, using jQuery .click and .ajax by triggers a PHP script and passing in the the database primary key ID.

I have this jQuery code so far:

$(document).ready(function() {
    $("#clickme").click(function() {
        $.post("removeDNFromBulk.php");
    );
});

But I need to pass in an ID to removeDNFromBulk.php, either as POST or GET, either suits.

I have this HTML to trigger the jQuery code

<a id="clickme" href="javascript:void(0);">click me</a>

Obviously I'm not passing in any ID to the jQuery but that's that part I can't get my head around. How can that be done?

Upvotes: 1

Views: 217

Answers (1)

PeeHaa
PeeHaa

Reputation: 72709

I would do something like this:

I would use a class because if I understand your question correct there are several clickme links. Id's should always be unique.

Also I have removed that ugly javascript:void(0) thingy :)

And I've added the id to the data attribute of the link.

<a class="clickme" href="#" data-id="{the id}">click me</a>

You can add data to the post like the following.

$(document).ready(function() {
    $('.clickme').click(function() {
      var del_id = $(this).data('id');
      $.post("removeDNFromBulk.php", { id: del_id } );

      return false;
    });
});

The return false prevents both the event bubbling up the DOM and the default behaviour of the click.

EDIT

You should wrap the code in a document ready to make sure the element is loaded before trying to access it.

Upvotes: 2

Related Questions