Search code examples
javascriptjqueryasp.netrepeaterdeclarative

Declaratively setting jQuery.Data property for a HTML link


Assuming I have a HTML link in my rows inside a datagrid or repeater as such

<a href="javascript:void(0);" class="DoSomething">DoSomething</a>

Now also assuming that I have handled the click event for all my DoSomethings in jQuery as such

$(".DoSomething").click(function (e) {
    //Make my DoSomethings do something
});

What is the correct technique for passing data to the click event that is dependent on the link clicked?

Without jQuery you would typically do something like this.

<a href="javascript:void(0);" class="DoSomething" onclick="DoSomething('<%# Eval("SomeValue") %>');">DoSomething</a>

but this technique obviously doesn't work in the jQuery case.

Basically my ideal solution would somehow add values for to the jQuery.Data property for the link clicked but doing so declaratively.


Solution

  • You could use the attr() function.

    http://api.jquery.com/attr/

    $("#Something").attr("your-value", "Hello World");
    
    $("#Something").click(function (e) {
        //Make my DoSomethings do something
       var value = $(this).attr("your-value");
       alert(value); // Alerts Hello World
    
    });