Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Problem With jQuery AppendTo and Data Attributes

Problem With jQuery AppendTo and Data Attributes

Scheduled Pinned Locked Moved JavaScript
helpcsharpjavascriptquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnalogNerd
    wrote on last edited by
    #1

    I've run into a rather interesting, and frustrating bug that I'm hoping someone has some experience with or can help me work around. Here's the scenario, and I've also set up a fiddle to show it [^] I have a div with data-location="1" If I move this div like this

    $("#myDiv").appendTo("#someOtherDiv");

    and then change the data-location value

    $("#myDiv").data("location", "newValue");

    I can no longer find the element using the data-location attribute. This returns 0

    $("[data-location='newValue']").length;

    This returns 1

    $("[data-location='oldValue']").length;

    BUT, this returns newValue

    $("#myDiv").data("location");

    Has anyone ever run into this? Am I missing something here, or doing something wrong? [EDIT: has some trouble with the fiddle, the link works now.] [EDIT: SOLVED] Apparently the .data in jquery only reads the attributes once and never sets the attributes. If you want to set data- attributes and recall them in your code you have to use the .attr() call.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups