user1464139
user1464139

Reputation:

Why can I not set my data attribute with jQuery?

I have the following:

editCity: "/Admin/Citys/Edit?pk=0001I&rk=5505005Z"

$('#editCity')
    .attr('title', "Edit City " + rk)
    .data('disabled', 'no')
    .data('href', editCity)
    .removeClass('disabled');

When I check the HTML with developer tools I see this:

<div class="button dialogLink" id="editCity" 
data-action="EditCity" data-disabled="yes" 
data-entity="City" title="Edit City 5505005Z" ></div>

Everything is updated except the href. Anyone have an ideas what I am doing wrong?

Upvotes: 0

Views: 1541

Answers (3)

Marcus Johansson
Marcus Johansson

Reputation: 2667

I think jQuery stores the data internally if they don't exist the first time you set them. If you really want to force it:

$("#editCity").attr("data-href",editCity)

Upvotes: 1

besluitloos
besluitloos

Reputation: 269

You cannot set a href attribute to a div. you could use data-href instead, or use a a-tag instead of a div.

Upvotes: -1

Bergi
Bergi

Reputation: 664434

Use

var editCity = "/Admin/Citys/Edit?pk=0001I&rk=5505005Z";

What you did was a labeled statement, consisting only of a string literal and missing a semicolon.


Btw, jQuery's .data() method is not to be used for data-attributes, but just for associating JS objects with DOM elements.

Upvotes: 1

Related Questions