1

How do i add custom attribute without a value jQuery? I want the following:

    <tr data-header-row>

The following does not work:

  $(tr).prop('data-header-row',true);

Thanks

1 Answer 1

2

To add HTML element attributes in jQuery you need to use attr method:

$(tr).attr('data-header-row','true');

But I would suggest you to use data method instead:

$(tr).data('header-row', true);

It will automatically add data- namespace to your attribute.

To get stored value then you can do simple:

console.log( $(tr).data('header-row') );
Sign up to request clarification or add additional context in comments.

8 Comments

.data() does not actually add an attribute, just saves the value and links it to the element in a jQuery internal cache.
Hello. That did not work. .attr puts an attribute with a value of true. I do not want that.
@Spokey correct, but using data you can grab attributes values without adding an preffix.
@BoundForGlory and what do you want then?
@BoundForGlory if you want to have just attribute you can pass an empty string to it: $(tr).attr('data-header-row','');
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.