0

how to create custom attribute for html tag.

Sample HTML Code:

<div class="header" custom-data-attribute="hide"> sample text </div>
yugi
  • 834
  • 1
  • 15
  • 28

3 Answers3

5

You can use data-* HTML5 attribute:

<div class="header" data-custom-attribute="hide"> sample text </div>

then apply .data() to set the value:

$('div.header').data("custom-attribute","value here");

and retrieve the value using:

var customAttr = $('div.header').data("custom-attribute");
Felix
  • 37,892
  • 8
  • 43
  • 55
  • This is the only answer from the view of OP. +1 – Bhojendra Rauniyar Apr 21 '14 at 10:30
  • +1 for quick and better solution. But we need to be careful while using data-attributes. http://stackoverflow.com/questions/22753629/jquery-get-html-5-data-attributes-with-hyphens-and-case-sensitivity – Kiran Apr 21 '14 at 10:35
1

Try using .attr() function,

$('div.header').attr('custom-data-attribute','hide');
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
0

You can use .attr() or .prop(). Try this:

 $('div.header').attr("custom-data-attribute","hide");

or

 $('div.header').prop("custom-data-attribute","hide");

Working Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125