Jquery – How to change the value of a custom attribute

jquery

var selector = $(this);

What is the proper code to change the custom attribute data-change-me for selector?

The syntax

selector[data-change-me='someValue'];

Is not working for me

Best Answer

I think you want the attr method.

selector.attr("data-change-me","someValue");

Here's the documentation: http://api.jquery.com/attr/

It's worth pointing out also that it looked like what you were trying to do was treat the jQuery wrapped DOM object as a Javascript object e.g:

Look at the following example for what I mean:

var myObject = {
     "data-change-me":"someValue";
};

myObject["data-change-me"] = "someOtherValue";

This is valid syntax for pure Javascript objects but not jQuery. To learn about Javascript I recommend Javascript the good parts