最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery add data attribute while creating dynamic dom elements - Stack Overflow

programmeradmin2浏览0评论

How to add data attribute while creating custom dynamic dom elements

like -

var div = $('#ajey');

  var text = "<div class='parent'>parent div";

  text +="<div class='child' data-child='true'>how do I add .data value for this div during on the fly dom creation?</div>";

  text += "</div>";

  div.html(text);

Fiddle here - Demo

Here I have added data-child this works, but when someone inspect elements via developer tools this is visible.

Where as the if I add via jquery .data() the data is not visible in the developer console.

But I am not able to figure out how to add data via jquery when I am creating elements on the fly.

How to add data attribute while creating custom dynamic dom elements

like -

var div = $('#ajey');

  var text = "<div class='parent'>parent div";

  text +="<div class='child' data-child='true'>how do I add .data value for this div during on the fly dom creation?</div>";

  text += "</div>";

  div.html(text);

Fiddle here - Demo

Here I have added data-child this works, but when someone inspect elements via developer tools this is visible.

Where as the if I add via jquery .data() the data is not visible in the developer console.

But I am not able to figure out how to add data via jquery when I am creating elements on the fly.

Share Improve this question edited Dec 24, 2015 at 20:22 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 16, 2014 at 10:36 AjeyAjey 8,22212 gold badges66 silver badges89 bronze badges 3
  • 1 jquery's .data doesn't actually add DOM data attributes but rather keeps the attributes in JS land. – Benjamin Gruenbaum Commented Nov 16, 2014 at 10:37
  • oh and how do I associate a dom's data attribute to be kept in the JS land ? while I am generating the dom element? – Ajey Commented Nov 16, 2014 at 10:39
  • Honestly I would stay away from data attributes - if you're generating them you can push them into an array of objects and keep data related to them outside of your presentation layer. You can use .attr if you must: $("<div ... />").attr("data-foo", "bar") – Benjamin Gruenbaum Commented Nov 16, 2014 at 10:45
Add a ment  | 

2 Answers 2

Reset to default 1

Updated the code and fiddle.

var div = $('#ajey');

      var text = "<div class='parent'>parent div";

      text +="<div class='child'>how do I add .data value for this div during on the fly dom creation?</div>";

      text += "</div>";


      div.html(text); 
div.find('.child').data('child', 'true'); //data-child attribute is added but will not show in DOM when user inspects element
console.log($('.child').data('child')); //you can see the value of data-child attribute in console to make it confirm that it is added.

Working fiddle - http://jsfiddle/Ashish_developer/v0qmbL5z/1/

var div = $('#ajey');

var text = "<div class='parent'>parent div";
text +="<div class='child' data-child='true'>how do I add .data value for this div during on the fly dom creation?</div>";
text += "</div>";

var parent = $(text);

parent.attr("data-foo", "bar");
parent.find('.child').attr("data-foo", "bar");

div.html( parent );

OR

var parent = $(text);

parent.data("foo", "bar");
parent.find('.child').data("foo", "bar");

div.html( parent );

console.log($('.parent').data('foo'));
console.log($('.parent').find('.child').data('foo'));

DEMO: http://jsbin./necoqo/1/

发布评论

评论列表(0)

  1. 暂无评论