views:

235

answers:

7

Hi Everyone,

I've just recently discovered the power of jQuery. Just a quick question though.

What is a replacement for onclick="DeleteSomething('THE ID NUMBER LOADED IN BY SERVER SIDE')" ?

Is there even a way somehow to pass custom information such as an ID to a jquery onclick? Or do i have to stay with the old fashioned way?

Thanks a lot!

Adam

+1  A: 
$(document).ready(function () {
  $('#selector').click(function() {
    //here goes your onclick code
  });
);

Please, post some markup for more help.

Also, you should read the Getting started with jQuery resources, linked in the main page of the library.

eKek0
+1  A: 

In jQuery you would more likely do something like:

$('a').click(function(){
    # code here
});

With 'a' being whatever selector you want to use to find the right link.

In response to your comment:

Probably the best way, as someone else mentioned, would be to provide the dynamic data in one of the attributes of the link:

<a rel="{id}" >

$('a').click(function(){
    deleteFunction($(this).attr('rel'));
});
seth
Right but how do i pass dynamic data into that function? Like say it was a $(".DeleteComment").click and you have multiple buttons with that class, and when clicked, they pass in the id to delete? previously i just inserted this directly into a parameter of the onclick="DeleteComment("the dynamic id")".
Adam
A: 

Instead of this:

<button onclick="DeleteSomething('THE ID NUMBER LOADED IN BY SERVER SIDE')">

Do this:

<button id="thing">
<script>
$(function() {
    $("#thing").click(function() {
        DeleteSomething('THE ID NUMBER LOADED IN BY SERVER SIDE');
    }
}
<script>
Bennett McElwee
+6  A: 

I usually use a rel="" for some extra data i might need attached to the button or whatnot.

for example

<input class="btnDelete" rel="34" value="Delete" />

then in jquery

$('.btnDelete').click(function() {
    DeleteMethod($(this).attr("rel"));
  });
John Boker
you can even give it some off the wall attribute other than rel (ie: deleteid) and access it the same way: $(this).attr("deleteid")
Nick Spiers
wow i didn't know i could do that. i think that's the solution!
Adam
A: 

It would much cleaner if you do it this way:

<tag class="someclass" prop1="id from serverside" />

$('.someclass').click(function(){
   var prop1 = $(this).attr('prop1');
   //DeleteSomething(prop1)
});
jerjer
+1  A: 

If you stick the ID of the object you want to delete in the rel parameter, you can do it this way:

<script type="text/javascript>

$('a.deleter').click(function(){
   if($(this).attr("rel") != ""){
      DeleteSomething($(this).attr("rel"));
   }
});

</script>
<a href="javascript:void(0)" rel="54" class="deleter">Delete Widget</a>
Iain Fraser
A: 

Use the server to replace {ID} with the actual ID.

HTML:

<form id="deleter">
  <input type="hidden" name="id" value="{ID}" \>
  <input type="submit" value="Delete" rel="{ID}" \>
</form>

jQuery:

$('form#deleter input[type="submit"]').click(function() {
  var id = $(this).attr('rel');
  DeleteSomething(id);
  return false;
}

Don't forget to implement the deleting server-side also for people who don't have Javascript enabled!

singingwolfboy