views:

74

answers:

4

I need to pass extra arguments to onclick handler. I can't decide which way is "better":

EDIT:
Context: I have a table that shows roster of an event. Each row has a 'delete' button. What is a better way to pass recordId to the delete-handler?

$('a.button').click(function() {
    var recordId = $(this).metadata().recordId;
    console.log(recordId);
});
...
<tr>...<a class='{recordId:1} button'>delete</a></tr>
<tr>...<a class='{recordId:2} button'>delete</a></tr>

or

function delete(recordId) {
    console.log(recordId);
}
...
<tr>....<a class='button' onclick='deleteRecord(1)'>Delete</a></tr>
<tr>....<a class='button' onclick='deleteRecord(2)'>Delete</a></tr>

What are the pros and cons for each option?

NOTE: I use a.button as a custom, CSS-styled button, it does not behave as a link.

EDIT:
I would appreciate alternative solutions as well, if you can argument the advantages of offered alternatives.

A: 
$('a.button').click(function() {
    var classes = $(this).attr('class').split(' ');
    var option;

    for( var i in classes )
    {
      if( classes[i].indexOf( 'option' ) != -1 )
      {
        option = classes[i].substr( 6 );
        break;
      }
    }

    console.log( option );
});
...
<a class='option-yes button'>Yes</a>    
<a class='option-no button'>No</a>
ovais.tariq
why not use metadata()?
this is a more browser friendly approach that would work on all browsers
ovais.tariq
and besides as far as i remember metadata is actually a separate jquery plugin, so why have the overhead of using an extra plugin when u can do it using classes.besides, metadata would be useful in situations where some good amount of data needs to be saved, here you are only saving some small values, so why do the metadata heavy lifting
ovais.tariq
is there browser that would choke on {} in class?
@ovais.tariq: which is exactly the reason I put the question. I am looking for cons/pros of each method. Your solution is an improvement(?) over metadata, but why is it better than onclick='delete(1234)'?
its better than onclick='delete(1234)' because there should be separation of functionality from the web page's markup or content, so that we do not fall into the trap of lack of scalability and non-reusable code, this kind of coding style is called "unobtrusive javascript'. You can read about the merits of this style here http://en.wikipedia.org/wiki/Unobtrusive_JavaScript
ovais.tariq
A: 
ntownsend
My bad. I have changed the example so it uses recordId instead of simple yes/no.
Not sure why you got that impression :) Data is inlined on the server.
+1  A: 

I would just go with your second approach - it's the simplest and there is nothing wrong with it.

serg
+3  A: 

Store the record id as an attribute of element itself, but instead of using the metadata plugin which stores it in a weird format, I would recommend you use HTML5's data attributes that is also backwards compatible.

A row would look like:

<tr> .. <a data-id="1">delete</a> .. </tr>

In the handler, retrieve the attribute value and act on it

function deleteRecord() {
    var rowId = $(this).attr('data-id');
    ...
}

It is comparable to using the metadata plugin, but it does not overload the class attribute. No extra plugins are needed for this. It uses a single handler just as the metadata plugin does which is performant for large datasets.

The inline onclick handlers are bad for the same reasons. A new handler is created per row. It cuts down on flexibility and is generally a bad practice.

Anurag
`data-` would be the best. There are no cons to using `data-` whatsoever? Every common browser will be cool with it?
None that I am aware of, especially in the context of the above criteria. Also I'm not sure how far back the support goes in terms of common browsers.
Anurag