0

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?

1
  • It'd be better to set the "title" property with .prop() instead of .attr() if you're using at least version 1.6 of jQuery. Commented Oct 9, 2012 at 13:26

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry maybe I was not clear. I do use var but the output I showed was from the debug when I hover over editCity.
@Anne when you use .data() to store properties with a DOM element, you won't see those in the debugger. They're stored in a map object internal to jQuery.
@Bergi - Now I am really confused. I thought .data("xxx","thedata") was the same as .attr("data-xxx","thedata");
@Bergi - As of jQuery 1.4.3 HTML 5 data- attributes will be automatically pulled in to jQuery's data object (from the documentation)
@Anne: No, it is not. There is no serialisation, it stores any js objects as "properties of the DOM element" (but in a memory-leak-save way). It uses the data-attributes only as a fallback if nothing associated is found.
|
1

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)

5 Comments

yes jQuery stores data on the Dom element and thats not reflected in the live html. the fact that jQuery do fall back to the data attributes in some cases do worry me
@VeXii jQuery only reads values from data-foo attributes. It does not update those attribute values.
that is "some cases" in my book, seen alot of confusion about that
@VeXii the library does not store values on DOM elements ever, unless code explicitly asks it to with .attr() or .prop(). Fetching from data-foo attributes is not risky.
and just how much "better" is it getting data attribues whith .data() then .prop() or .attr()? and is it worth the confusion?
-1

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

3 Comments

He's not setting an "href" attribute. He's setting a "data" property, and those are not implemented as attributes.
Well does my use of .data not try to set it as data-href ?
@Anne no absolutely not - the .data() mechanism will read initial values from data-xyz attributes, but it will never update those attributes. The .data() mechanism is completely internal to jQuery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.