MissCoder87
MissCoder87

Reputation: 2669

Getting data-id from jquery

I'm using ASP.net/C# with jQuery and i'm trying to get the value in data-id. I have the following code:

Markup:

 <asp:Button runat="server" ID="btnAddLocations" Text="Update" CssClass="btn btn-info pull-right addLocation" />

Code behind (basically adds the data-id attribute)

SPID = "3"
btnAddLocations.Attributes.Add("data-id", SPID);

jQuery:

$(".addLocation").click(function () {
        var id = ($(".addLocation").data("spid"));
        alert(id);
        var url = "popups/AddLocations.aspx?spid=" + id;
        window.open(url, "Add Locations", "width=600,height=550");
        return false;
    });

However, ID always comes back as undefined

Upvotes: 1

Views: 191

Answers (3)

la elah ala allah
la elah ala allah

Reputation: 7

there is more easy way to pass id from button to javascript

   function DoEdit(e)
   {
       alert(e.toString());
   };

< input id="btnAddLocations" onclick="DoEdit('<%# Eval("SPID") %>')" value="GetID"/>

Upvotes: 0

Gajendra Naidu
Gajendra Naidu

Reputation: 405

I think you try to get value set as 'data-spid' in HTML DOM

SPID = "3"
btnAddLocations.Attributes.Add("data-spid", SPID);

Upvotes: 0

Satpal
Satpal

Reputation: 133443

I think you should use

var id = $(this).data("id");

OR

var id = $(this).attr("data-id");

Upvotes: 3

Related Questions